本文介绍了使用DropDownLists在GridView中添加动态行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hai,
当我从gridview删除行时,下拉值和文本框值被重置,以下是我用于删除的代码
hai,
When i delete the row from gridview,drop down values and textbox values are getting reset,Following is the code which i used for deleting
protected void RemoveButton_Click(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
int rowID = gvRow.RowIndex+1;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 1)
{
if (gvRow.RowIndex < dt.Rows.Count - 1)
{
//Remove the Selected Row data
dt.Rows.Remove(dt.Rows[rowID]);
}
}
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
//Set Previous Data on Postbacks
SetPreviousData();
}
请帮我解决这个问题.
please help me out this.
推荐答案
LinkButton lb = (LinkButton)sender;
GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
int rowID = gvRow.RowIndex + 1;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 1)
{
// Store value in Data Table When Row is Remove From grid view
for(int i=0;i<dt.rows.count;i++)>
{
if (i != (rowID - 1))
{
// Make changes as per column name for data table & controls name in FindControl()
dt.Rows[i][colID]= ((TextBox)GridView1.Rows[i].FindControl("TextBoxID")).Text;
dt.Rows[i][colName]= ((TextBox)GridView1.Rows[i].FindControl("TextBoxName")).Text;
dt.Rows[i][colIsActive]= ((CheckBox)GridView1.Rows[i].FindControl("checkboxActive")).Checked;;
}
}
dt.Rows.Remove(dt.Rows[rowID]);
Gridview1.DataSource = dt;
Gridview1.DataBind();
ViewState["CurrentTable"] = dt;
}
}
这篇关于使用DropDownLists在GridView中添加动态行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!