简体   繁体   English

gridview添加新行

[英]gridview adding a new row

I have a gridview with item templates bound to an sql data source. 我有一个绑定到sql数据源的项目模板的gridview。 I need to add a new row on the textchanged event of my last textbox itemtemplate in the specific row. 我需要在特定行中最后一个文本框itemtemplate的textchanged事件上添加新行。 the problem is when i add a new row, the previous row values, entered by me are not displayed. 问题是当我添加新行时,我输入的前一行值不显示。 Can someone please give me the code to retain those values. 有人可以给我代码保留这些值。 please keep the code in c#. 请将代码保存在C#中。

thanks 谢谢

SqlDataAdapter ddm = new SqlDataAdapter(cdm);
DataTable dss = new DataTable();
ddm.Fill(dss);
GridView1.DataSource = dss;
GridView1.DataBind();
for (int i = 0; i <= dss.Rows.Count - 1; i++)
{
   DropDownList dd1 = (DropDownList)GridView1.Rows[i].Cells[1].FindControl("ddl1");

   dd1.DataSource = dss;
   dd1.DataTextField = "Material_Details";
   dd1.DataBind();
}

cn.Close();
//code for new row

DataRow dr = dss.NewRow;
dss.Rows.Add(dr); 

From what I understand, you are entering values and then trigger a post-back that adds a new row. 据我了解,您正在输入值,然后触发回发以添加新行。 You need during the post-back to grab the values you entered and save / redisplay them, or they are lost. 在回发期间,您需要获取输入的值并保存 /重新显示它们,否则它们将丢失。

You save them the same as any other way, by looking for their _Changed event handlers (or in Load() with an if(IsPostBack){ save values } ) and saving them / adding them to your DataTable to be displayed in the grid when you re-bind it. 您可以通过与其他方式相同的方式来保存它们,方法是查找它们的_Changed事件处理程序(或在带有if(IsPostBack){ save values } Load()中)并将它们保存/添加到DataTable中,以便在出现时在网格中显示您重新绑定了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM