Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dynamic grid with a template control. I have two buttons: ADD and REMOVE. When I bind the grid and click on the Add button, the last row data is displayed in the grid and all data is removed, and a blank row is created.

Here is my code:
int rowIndex = 0;

if (ViewState["CurrentTable1"] != null)
{

   DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable1"];
   DataTable DtLast = null;
   DataRow drCurrentRow = null;

   if (dtCurrentTable.Rows.Count > 0)
   {
       for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
       {
           //extract the TextBox values

           TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].
                Cells[1].FindControl("TxtName");
           TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].
                Cells[2].FindControl("TxtStatus");
           TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].
                Cells[3].FindControl("TxtAddress");
           drCurrentRow = dtCurrentTable.NewRow();
           drCurrentRow["SrNo"] = i + 1;
           drCurrentRow["Name"] = box1.Text;
           drCurrentRow["Status"] = box2.Text;
           drCurrentRow["Address"] = box3.Text;
           rowIndex++;
           DtLast.Rows.Add(drCurrentRow);
       }

       //add new row to DataTable
                  

       //Store the current data to ViewState
       ViewState["CurrentTable1"] = dtCurrentTable;

       //Rebind the Grid with the current data
       Gridview1.DataSource = dtCurrentTable;
       Gridview1.DataBind();
    }
}

Can anyone please help me?
Posted
Updated 8-Feb-10 6:34am
v5

In first place, you should not use Viewstate to store datagrid data in it. It will make page a lot heavy and this will hit performance.

Instead use Caching.
You need a rebind to the source after the change. Either fetch the data again or make the necessary change in the cache/dataset and rebind it to the grid.
 
Share this answer
 
you are storing & rebinding grid with the same Datatable[dtCurrentTable] which is from Viewstate

so store & bind the Datatable DtLast
//Store the current data to ViewState
ViewState["CurrentTable1"] = DtLast;
//Rebind the Grid with the current data
Gridview1.DataSource = DtLast;
Gridview1.DataBind();
 
Share this answer
 

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