Hai
i'm not exactly sure this is what you need as solution,here is my what i found in your method
Solution 1 - Alter Your Code like below
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("Date");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("Description");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("Hours");
if (String.IsNullOrEmpty(dt.Rows[i]["Column1"].ToString()))
{
if (!String.IsNullOrEmpty(box1.Text))
{
dt.Rows[i]["Column1"] = box1.Text;
}
}
if (String.IsNullOrEmpty(dt.Rows[i]["Column2"].ToString()))
{
if (!String.IsNullOrEmpty(box1.Text))
{
dt.Rows[i]["Column2"] = box2.Text;
}
}
if (String.IsNullOrEmpty(dt.Rows[i]["Column3"].ToString()))
{
if (!String.IsNullOrEmpty(box1.Text))
{
dt.Rows[i]["Column3"] = box3.Text;
}
}
box1.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
box3.Text = dt.Rows[i]["Column3"].ToString();
rowIndex++;
}
}
}
}
Solution 2 : Create a Method like this and call in your method
SUBMIT_Click
Advantages : - While your are reading from below lines of code : - if view state is expired then also u are able to read all the data directly from your datagrid
2: - after adding second row in your grid if u r changing data in your first row then the above method will give you a head ache of un-updated data in your view state
if u r using below method all the updated data will be
private DataTable ReadAllGridValues()
{
DataTable dt = InitializeDataTable();
int rowIndex = 1;
DataRow dr = null;
for(int rowCount=0;rowCount<Gridview1.Rows.Count;rowCount++)
{
TextBox box1 = (TextBox)Gridview1.Rows[rowCount].Cells[1].FindControl("Date");
TextBox box2 = (TextBox)Gridview1.Rows[rowCount].Cells[2].FindControl("Description");
TextBox box3 = (TextBox)Gridview1.Rows[rowCount].Cells[3].FindControl("Hours");
dr = dt.NewRow();
dr["RowNumber"] = rowIndex + 1;
dt.Rows[rowCount]["Column1"] = box1.Text;
dt.Rows[rowCount]["Column2"] = box2.Text;
dt.Rows[rowCount]["Column3"] = box3.Text;
dt.Rows.Add(dr);
}
return dt;
}
private DataTable InitializeDataTable()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dt.Rows.Add(dr);
return dt;
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
dt = InitializeDataTable();
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
protected void SUBMIT_Click(object sender, EventArgs e)
{
DataTable dt = ReadAllGridValues();
foreach (DataRow dr in dt.Rows)
{
}
}
*Note :- Kindly please Comment back if it had resolved your problem