在 asp.net 中向网格视图中添加新行

本文关键字:视图 添加 新行 网格 asp net | 更新日期: 2023-09-27 18:33:39

当我添加新行时,我希望新行的默认值为零。当我单击"添加新行"按钮时,现在添加了新行,但它们只是空白的。 如何为行中的每一列预先分配零值?

 private void AddNewRow()
    {
      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++)
          {
            DropDownList ddlTaskCurrWeek = (DropDownList)myGridView.Rows[rowIndex].Cells[1].FindControl("ddlTaskCurrWeek");
            TextBox TextBoxDay1 = (TextBox)myGridView.Rows[rowIndex].Cells[2].FindControl("txtDay1");
            TextBox TextBoxDay2 = (TextBox)myGridView.Rows[rowIndex].Cells[3].FindControl("txtDay2");
            TextBox TextBoxDay3 = (TextBox)myGridView.Rows[rowIndex].Cells[4].FindControl("txtDay3");
            TextBox TextBoxDay4 = (TextBox)myGridView.Rows[rowIndex].Cells[5].FindControl("txtDay4");
            TextBox TextBoxDay5 = (TextBox)myGridView.Rows[rowIndex].Cells[6].FindControl("txtDay5");
            TextBox TextBoxDay6 = (TextBox)myGridView.Rows[rowIndex].Cells[7].FindControl("txtDay6");
            TextBox TextBoxDay7 = (TextBox)myGridView.Rows[rowIndex].Cells[8].FindControl("txtDay7");
            Label lbl8 = (Label)myGridView.Rows[rowIndex].Cells[9].FindControl("lblTotal");
            // TextBox TextBoxDay8 = (TextBox)myGridView.Rows[rowIndex].Cells[9].FindControl("txtDay8");

            drCurrentRow = dtCurrentTable.NewRow();
            drCurrentRow["RowNumber"] = i + 1;
            dtCurrentTable.Rows[i - 1]["Col1"] = ddlTaskCurrWeek.SelectedValue;
            dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxDay1.Text;
            dtCurrentTable.Rows[i - 1]["Col3"] = TextBoxDay2.Text;
            dtCurrentTable.Rows[i - 1]["Col4"] = TextBoxDay3.Text;
            dtCurrentTable.Rows[i - 1]["Col5"] = TextBoxDay4.Text;
            dtCurrentTable.Rows[i - 1]["Col6"] = TextBoxDay5.Text;
            dtCurrentTable.Rows[i - 1]["Col7"] = TextBoxDay6.Text;
            dtCurrentTable.Rows[i - 1]["Col8"] = TextBoxDay7.Text;
            dtCurrentTable.Rows[i - 1]["Col9"] = lbl8.Text;
            //  dtCurrentTable.Rows[i - 1]["Col9"] = TextBoxDay8.Text;

            rowIndex++;
          }
          dtCurrentTable.Rows.Add(drCurrentRow);
          ViewState["CurrentTable"] = dtCurrentTable;
          myGridView.DataSource = dtCurrentTable;
          myGridView.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++)
          {
            DropDownList ddlTaskCurrWeek = (DropDownList)myGridView.Rows[rowIndex].Cells[1].FindControl("ddlTaskCurrWeek");
            TextBox TextBoxDay1 = (TextBox)myGridView.Rows[rowIndex].Cells[2].FindControl("txtDay1");
            TextBox TextBoxDay2 = (TextBox)myGridView.Rows[rowIndex].Cells[3].FindControl("txtDay2");
            TextBox TextBoxDay3 = (TextBox)myGridView.Rows[rowIndex].Cells[4].FindControl("txtDay3");
            TextBox TextBoxDay4 = (TextBox)myGridView.Rows[rowIndex].Cells[5].FindControl("txtDay4");
            TextBox TextBoxDay5 = (TextBox)myGridView.Rows[rowIndex].Cells[6].FindControl("txtDay5");
            TextBox TextBoxDay6 = (TextBox)myGridView.Rows[rowIndex].Cells[7].FindControl("txtDay6");
            TextBox TextBoxDay7 = (TextBox)myGridView.Rows[rowIndex].Cells[8].FindControl("txtDay7");
            Label lbl8 = (Label)myGridView.Rows[rowIndex].Cells[9].FindControl("lblTotal");
            // TextBox TextBoxDay8 = (TextBox)myGridView.Rows[rowIndex].Cells[9].FindControl("txtDay8");
            // drCurrentRow["RowNumber"] = i + 1;
            myGridView.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
            ddlTaskCurrWeek.SelectedValue = dt.Rows[i]["Col1"].ToString();
            TextBoxDay1.Text = dt.Rows[i]["Col2"].ToString();
            TextBoxDay2.Text = dt.Rows[i]["Col3"].ToString();
            TextBoxDay3.Text = dt.Rows[i]["Col4"].ToString();
            TextBoxDay4.Text = dt.Rows[i]["Col5"].ToString();
            TextBoxDay5.Text = dt.Rows[i]["Col6"].ToString();
            TextBoxDay6.Text = dt.Rows[i]["Col7"].ToString();
            TextBoxDay7.Text = dt.Rows[i]["Col8"].ToString();
            lbl8.Text = dt.Rows[i]["Col9"].ToString();
            rowIndex++;
          }
        }
      }
    }

在 asp.net 中向网格视图中添加新行

首先,我认为你现有的逻辑有问题。 您似乎正在迭代表中的所有现有行,并且在每次迭代时,您都在当前索引 + 1 处创建一个新行。 但是您只在最后将此行添加到表中。 这意味着,如果表中有 10 个现有行,则实际上正在创建 10 个新行,但只有最后一次迭代才实际向表中添加了一行,因此净影响是 1 个新行。 如果这不是您的意图,更好的方法是:

//outside the loop
drNewRow = dtCurrentTable.NewRow();
drNewRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
drNewRow["Col1"] = "0"; //assuming your drop down has a default value of 0.
drNewRow["Col2"] = "0";
//etc..
dtCurrentTable.Rows.Add(drNewRow);

您正在使用以下命令从表单域初始化新行:

      drCurrentRow = dtCurrentTable.NewRow();
      drCurrentRow["RowNumber"] = i + 1;
      dtCurrentTable.Rows[i - 1]["Col1"] = ddlTaskCurrWeek.SelectedValue;
      dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxDay1.Text;
      dtCurrentTable.Rows[i - 1]["Col3"] = TextBoxDay2.Text;
      dtCurrentTable.Rows[i - 1]["Col4"] = TextBoxDay3.Text;
      dtCurrentTable.Rows[i - 1]["Col5"] = TextBoxDay4.Text;
      dtCurrentTable.Rows[i - 1]["Col6"] = TextBoxDay5.Text;
      dtCurrentTable.Rows[i - 1]["Col7"] = TextBoxDay6.Text;
      dtCurrentTable.Rows[i - 1]["Col8"] = TextBoxDay7.Text;
      dtCurrentTable.Rows[i - 1]["Col9"] = lbl8.Text;

相反,请使用您希望它们具有的内容初始化它们:

      drCurrentRow = dtCurrentTable.NewRow();
      drCurrentRow["RowNumber"] = i + 1;
      dtCurrentTable.Rows[i - 1]["Col1"] = "0";
      dtCurrentTable.Rows[i - 1]["Col2"] = "0";

等。。。

我在这里假设列正在寻找字符串值,因为代码的设置方式,但您可以设置任何内容。

您还需要将行添加到数据集。 您的代码为您提供了一行,您可以使用该行。 但是数据集还不知道它(是的,我知道,你会认为它会,但它没有)

所以,你还需要

dtCurrentTable.Rows.Add(drCurrentRow);

地方。 我会把它放在列初始化之后

https://msdn.microsoft.com/en-us/library/5ycd1034.aspx

如前所述,您正在初始化每一行。