Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Using this example, I have a problem.
I have a gridview:
ASP
<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:
C#
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:
C#
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:
C#
>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.
Posted
Updated 20-Nov-19 4:57am
v3
Comments
Herman<T>.Instance 20-Nov-19 7:57am    
I guess the problem is the viewstate. It is set once and resued. I do not see any code where the ViewState is updated after adding a row.
DrgIonuţ 20-Nov-19 8:06am    
Ok. But a new row is added with the text from button 2. The problem is that row 1 is updated with the message from row 2.
Herman<T>.Instance 20-Nov-19 8:08am    
Yeah but the first new row is not added to the viewstate, so you add the second row to the original viewstate. If you put a breakpoint @ DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; you will see....
DrgIonuţ 20-Nov-19 8:27am    
To see what? Nothing seems to be wrong.
CHill60 20-Nov-19 7:58am    
If you use the debugger and step into your AddNewRow function you will see that you are going into the loop that sets every row in your datatable to the text passed it.

1 solution

Try something like the following (caveat - untested)
C#
DataRow drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["Col1"] = s;
drCurrentRow["Col2"] = s1;
dtCurrentTable.Rows.Add(drCurrentRow);
 
Share this answer
 
Comments
DrgIonuţ 21-Nov-19 3:02am    
Thank you for your reply, CHill60!
My AddNewRow function now looks like:
private void AddNewRow(string strg, string strg1)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;

drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["Col1"] = strg;
drCurrentRow["Col2"] = strg1;
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
A new row is added, but the text doesn't appear. I also have tried to replace "strg" and "strg1" with "ab" and "cd", but the result is the same.
CHill60 21-Nov-19 3:56am    
try drCurrentRow["Message"].Value = s etc. Use the debugger to check that the DataTable is correctly populated and you are using the correct column names. If that is ok then it's the databinding or the way you have set up your gridview.
I'm sorry - I don't have Visual Studio here at the moment so I can't debug your code
DrgIonuţ 21-Nov-19 4:16am    
"Value" property doesn't exist. Thanks! :)

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