Click here to Skip to main content
15,896,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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.
Posted
Updated 28-Aug-12 19:16pm
v2
Comments
Sandeep Mewara 29-Aug-12 2:03am    
What do you mean by reset here?
Malarpalanisamy 29-Aug-12 8:51am    
reset means there is empty value

1 solution

- Re Write block of code following way

C#
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;

     }

}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900