【问题标题】:add new row in gridview after binding C#, ASP.net绑定 C#、ASP.net 后在 gridview 中添加新行
【发布时间】:2013-10-29 10:29:04
【问题描述】:

点击下面的链接按钮时,我想在绑定后向gridview添加一个新的空白行,如图所示。如果在其中输入了任何数据,gridview 内的文本框应该保持不变。只想添加一行。请帮我。提前致谢。

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    你可以试试下面的代码

    protected void Button1_Click(object sender, EventArgs e)
       {
           DataTable dt = new DataTable();
    
           if (dt.Columns.Count == 0)
           {
               dt.Columns.Add("PayScale", typeof(string));
               dt.Columns.Add("IncrementAmt", typeof(string));
               dt.Columns.Add("Period", typeof(string));
           }
    
           DataRow NewRow = dt.NewRow();
           NewRow[0] = TextBox1.Text;
           NewRow[1] = TextBox2.Text;
           dt.Rows.Add(NewRow); 
           GridView1.DataSource = dt;
           GridViewl.DataBind();
       }
    

    这里 payscale、incrementamt 和 period 是数据库字段名称。

    【讨论】:

      【解决方案2】:

      你可以直接运行这个例子。

      aspx 页面:

      <asp:GridView ID="grd" runat="server" DataKeyNames="PayScale" AutoGenerateColumns="false">
          <Columns>
              <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Pay Scale">
                  <ItemTemplate>
                      <asp:TextBox ID="txtPayScale" runat="server" Text='<%# Eval("PayScale") %>'></asp:TextBox>
                  </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Increment Amount">
                  <ItemTemplate>
                      <asp:TextBox ID="txtIncrementAmount" runat="server" Text='<%# Eval("IncrementAmount") %>'></asp:TextBox>
                  </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Period">
                  <ItemTemplate>
                      <asp:TextBox ID="txtPeriod" runat="server" Text='<%# Eval("Period") %>'></asp:TextBox>
                  </ItemTemplate>
              </asp:TemplateField>
          </Columns>
      </asp:GridView>
      <asp:Button ID="btnAddRow" runat="server" OnClick="btnAddRow_Click" Text="Add Row" />
      

      C#代码:

      protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              grd.DataSource = GetTableWithInitialData(); // get first initial data
              grd.DataBind();
          }
      }
      
      public DataTable GetTableWithInitialData() // this might be your sp for select
      {
          DataTable table = new DataTable();
          table.Columns.Add("PayScale", typeof(string));
          table.Columns.Add("IncrementAmount", typeof(string));
          table.Columns.Add("Period", typeof(string));
      
          table.Rows.Add(1, "David", "1");
          table.Rows.Add(2, "Sam", "2");
          table.Rows.Add(3, "Christoff", "1.5");
          return table;
      }
      
      protected void btnAddRow_Click(object sender, EventArgs e)
      {
          DataTable dt = GetTableWithNoData(); // get select column header only records not required
          DataRow dr;
      
          foreach (GridViewRow gvr in grd.Rows)
          {
              dr = dt.NewRow();
      
              TextBox txtPayScale = gvr.FindControl("txtPayScale") as TextBox;
              TextBox txtIncrementAmount = gvr.FindControl("txtIncrementAmount") as TextBox;
              TextBox txtPeriod = gvr.FindControl("txtPeriod") as TextBox;
      
              dr[0] = txtPayScale.Text;
              dr[1] = txtIncrementAmount.Text;
              dr[2] = txtPeriod.Text;
      
              dt.Rows.Add(dr); // add grid values in to row and add row to the blank table
          }
      
          dr = dt.NewRow(); // add last empty row
          dt.Rows.Add(dr);
      
          grd.DataSource = dt; // bind new datatable to grid
          grd.DataBind();
      }
      
      public DataTable GetTableWithNoData() // returns only structure if the select columns
      {
          DataTable table = new DataTable();
          table.Columns.Add("PayScale", typeof(string));
          table.Columns.Add("IncrementAmount", typeof(string));
          table.Columns.Add("Period", typeof(string));
          return table;
      }
      

      【讨论】:

        【解决方案3】:
        protected void TableGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           if (e.Row.RowIndex == -1 && e.Row.RowType == DataControlRowType.Header)
           {
              GridViewRow gvRow = new GridViewRow(0, 0, DataControlRowType.DataRow,DataControlRowState.Insert);
              for (int i = 0; i < e.Row.Cells.Count; i++)
              {
                 TableCell tCell = new TableCell();
                 tCell.Text = "&nbsp;";
                 gvRow.Cells.Add(tCell);
                 Table tbl = e.Row.Parent as Table;
                 tbl.Rows.Add(gvRow);
              }
           }
        }
        

        【讨论】:

        • 请在你的答案中给出解释,而不是仅仅一段代码来帮助用户学习。请参阅How to answer 了解更多信息。感谢您的加入!
        【解决方案4】:

        尝试使用克隆技术。

        {
            DataGridViewRow row = (DataGridViewRow)yourdatagrid.Rows[0].Clone();
            // then for each of the values use a loop like below.
            int cc = yourdatagrid.Columns.Count;
            for (int i2 = 0; i < cc; i2++)
            {
                row.Cells[i].Value = yourdatagrid.Rows[0].Cells[i].Value;
            }
            yourdatagrid.Rows.Add(row);
            i++;
            }
        }
        

        这应该可行。我不确定绑定是如何工作的。希望它不会阻止这个工作。

        【讨论】:

        • DataGridViewRow 来自 winforms,如果我是正确的,这个问题是关于 asp.net webforms 中的 gridview 的。
        【解决方案5】:

        如果是使用dataset绑定到Grid中,可以在填写sql数据适配器后添加行:

        adapter.Fill(ds);
        ds.Tables(0).Rows.Add();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-12-26
          • 2013-02-25
          • 1970-01-01
          • 1970-01-01
          • 2017-11-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多