C# 使用按钮在gridview控件中添加新空行时出现问题

C# 使用按钮在gridview控件中添加新空行时出现问题,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个gridview,它永远处于编辑模式PageLoad。我在gridview的页脚添加了一个按钮,以便添加新行以插入值。现在的问题是,当我想要编辑数据库中的数据时。数据将绑定到gridview中,如果我单击按钮添加新行,gridview中已经存在的数据将消失,但第一行除外 这是“添加新行”按钮下的代码: private void AddNewRowToGrid() { int rowIndex = 0; if (ViewState["TempTab

我有一个gridview,它永远处于编辑模式PageLoad。我在gridview的页脚添加了一个按钮,以便添加新行以插入值。现在的问题是,当我想要编辑数据库中的数据时。数据将绑定到gridview中,如果我单击按钮添加新行,gridview中已经存在的数据将消失,但第一行除外

这是“添加新行”按钮下的代码:

private void AddNewRowToGrid()
    {
        int rowIndex = 0;

        if (ViewState["TempTable"] != null)
        {
            // Get TempTable from viewstate
            var tempTable = (DataTable)ViewState["TempTable"];
            DataRow tempRow = null;

            if (tempTable.Rows.Count > 0)
            {
                for (int i = 1; i <= tempTable.Rows.Count; i++)
                {
                    // Get Grid's values                    
                    var txtProblem =
                        (TextBox)gvTroubleshooting.Rows[rowIndex].Cells[2].FindControl("txtProblem");
                    var txtCorrectiveMessure =
                        (TextBox)gvTroubleshooting.Rows[rowIndex].Cells[3].FindControl("txtCorrectiveMessure");

                    // Create new row and update Row Number
                    tempRow = tempTable.NewRow();
                    tempTable.Rows[i - 1]["Problem"] = txtProblem.Text;
                    tempTable.Rows[i - 1]["Corrective_Measures"] = txtCorrectiveMessure.Text;

                    rowIndex++;
                }
                // Add data to datatable and viewstate
                tempTable.Rows.Add(tempRow);
                ViewState["TempTable"] = tempTable;

                // Attach Gridview Datasource to datatable
                gvTroubleshooting.DataSource = tempTable;
                gvTroubleshooting.DataBind();
            }
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
    }
SetPreviousData代码如下所示:

private void SetPreviousData()
    {
        int rowIndex = 0;

        if (ViewState["TempTable"] != null)
        {
            var tempTable = (DataTable)ViewState["TempTable"];

            if (tempTable.Rows.Count > 0)
            {
                for (int i = 0; i < tempTable.Rows.Count; i++)
                {
                    var txtProblem =
                        (TextBox)gvTroubleshooting.Rows[rowIndex].Cells[2].FindControl("txtProblem");
                    var txtCorrectiveMessure =
                        (TextBox)gvTroubleshooting.Rows[rowIndex].Cells[3].FindControl("txtCorrectiveMessure");

                    txtProblem.Text = tempTable.Rows[i]["Problem"].ToString();
                    txtCorrectiveMessure.Text = tempTable.Rows[i]["Corrective_Measures"].ToString();

                    rowIndex++;
                }
            }
        }
    }

非常感谢您的帮助

尝试将现有datagridview数据添加到临时datatable中,并将其与新添加的datatable合并。现在将合并的datatable设置为datagridview的数据源

在页脚上放置空文本框,然后在单击按钮时添加新项更简单。你能做到吗?我不想那样做,因为这只是他们添加更多内容的一种选择。基本上它是一个编辑表单