Using
this example, I have a problem.
I have a gridview:
<asp:GridView ID="GridView1" runat="server"
ShowFooter="True" AutoGenerateColumns="False" Width="100%" Height="100%">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Nr.">
<ControlStyle BorderStyle="None" />
</asp:BoundField>
<asp:TemplateField HeaderText="Message">
<ItemTemplate>
<asp:TextBox Native="True" ID="txtName" runat="server"></asp:TextBox>
</ItemTemplate>
<ControlStyle BorderStyle="None" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Data">
<ItemTemplate>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
</ItemTemplate>
<ControlStyle BorderStyle="None" />
</asp:TemplateField>
</Columns>
</asp:GridView>
and 2 buttons. When I click each button, a new row is inserted in Gridview. To create and bind data tot GridView, I have 2 functions:
private void AddNewRow(string s, string s1)
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox TextBoxName =
(TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtName");
TextBoxName.Text = s;
TextBox TextBoxAge =
(TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("txtAge");
TextBoxAge.Text = s1;
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxName.Text;
dtCurrentTable.Rows[i -1]["Col2"] = TextBoxAge.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}
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 TextBoxName = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtName");
TextBox TextBoxAge = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("txtAge");
TextBoxName.Text = dt.Rows[i]["Col1"].ToString();
TextBoxAge.Text = dt.Rows[i]["Col2"].ToString();
rowIndex++;
}
}
}
}
When I click on button 1, data is bound and a new row is created:
protected void Button1_Click(object sender, EventArgs e)
{ DateTime data = DateTime.Now;
string dt = data.ToString();
AddNewRow("Start", dt);
}
When I click on button 2, data is bound and a new row is created:
>protected void Button2_Click(object sender, EventArgs e)
{ DateTime data = DateTime.Now;
string dt = data.ToString();
AddNewRow("Stop", dt);
}
Let's say that first I click on button 1. Everything is ok. First row contains the message "Start" and the corresponding date. Also a new row is created. If I press button 2, row 2 contains the message "Stop" and the corresponding date, but also first row is overwritten with the message "Stop". What should I do to don't overwrite the first row?
What I have tried:
What I tried is already shown.