在asp.net中向gridview添加新行

时间:2016-02-08 18:43:04

标签: c# asp.net gridview

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

 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();
    }

这里是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++;
          }
        }
      }
    }

2 个答案:

答案 0 :(得分:1)

首先,我认为您的现有逻辑存在问题。您似乎正在迭代表中的所有现有行,并且在每次迭代时,您将在当前索引+ 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);

答案 1 :(得分:0)

您正在使用以下内容从表单字段初始化新行:

      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

正如已经提到的那样,你正在为每一行进行初始化。