【问题标题】:ASP.NET Grid View Add new rowASP.NET 网格视图添加新行
【发布时间】:2009-12-11 23:56:29
【问题描述】:

我刚刚开始使用 ASP.NET GridView 控件。

我正在考虑将"Add New Row" Button 添加到Footer 以将新的DataRow 添加到GridView

最初,我希望网格为empty,只显示一个页脚行。但是,如果没有数据行,则不会出现整个GridView,并且由于页脚也不会显示,因此无法添加第一行。

有没有办法显示一个只有页脚而没有数据行的GridView,还是我必须求助于kludge?

【问题讨论】:

    标签: asp.net gridview row add


    【解决方案1】:

    您是否考虑过对 GridView 进行子类化并覆盖其 CreateChildControls 方法(如果需要,还可能还有一些渲染方法)?

    您可以更改其默认行为。如果这是可能的,它会比添加空行更简洁。

    【讨论】:

    • 好建议。但肯定是以前做过的,最好是在VB.NET中,所以我可以根据需要学习和修改。
    • 如果以前做过,谷歌肯定会有所作为。如果做不到这一点,如果需要,你总是有 IntelliSense 和 Redgate Reflector。
    【解决方案2】:

    如果没有任何行,则 ASP.NET DataGrid 将不显示任何内容(或者如果您指定,也可以选择仅显示“无数据文本”值)。即使不存在数据或行,我们也希望至少显示网格标题。我们过去做过的一个技巧是在网格中添加一个空行。这将导致页眉/页脚出现。在标题的情况下,我们在空行上放置了一个 div,其中包含一些格式精美的文本……只是为了美化它。

    【讨论】:

    • +1 是的,我在想这个,但我认为这是一个杂牌。我很惊讶网格不支持它。还在寻找...
    【解决方案3】:

    我不知道这是否可行。但是我们在 Telerik RadGrid 中遇到了类似的问题-> 如果 datasource = null,则网格不显示。出于某种原因,虽然它有一个“无记录模板”,但它不起作用。设置 datasource = new object[]{} 成功了,它显示了一个空网格。我的 2 美分

    【讨论】:

      【解决方案4】:

      这里使用这个代码

        protected void Page_Load(object sender, EventArgs e)
          {
              lblError.Text = "";
              if (!IsPostBack)
              {
                  SetInitialRow();  
              }
      
          }
       private void SetInitialRow()
          {
      
              DataTable dt = new DataTable();
              DataRow dr = null;
              dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
              dt.Columns.Add(new DataColumn("Column1", typeof(string)));
      
      
              dr = dt.NewRow();
              dr["RowNumber"] = 1;
      
              dt.Rows.Add(dr);
              dt.TableName = "Coupons";
              //Store the DataTable in ViewState
              ViewState["CurrentTable"] = dt;
      
              Gridview1.DataSource = dt;
              Gridview1.DataBind();
      
          }
      
          private void AddNewRowToGrid()
          {
      
              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++)
                      {
                          //extract the TextBox values
                          TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
      
                          drCurrentRow = dtCurrentTable.NewRow();
                          drCurrentRow["RowNumber"] = i + 1;
                          drCurrentRow["Column1"] = box1.Text;
      
                          rowIndex++;
      
                      }
      
                      //add new row to DataTable
                      dtCurrentTable.Rows.Add(drCurrentRow);
                      //Store the current data to ViewState
                      ViewState["CurrentTable"] = dtCurrentTable;
      
                      //Rebind the Grid with the current data
                      Gridview1.DataSource = dtCurrentTable;
                      Gridview1.DataBind();
                  }
              }
              else
              {
                  Response.Write("ViewState is null");
              }
      
              //Set Previous Data on Postbacks
              SetPreviousData();
          }
          private void SetPreviousData()
          {
      
              int rowIndex = 0;
              if (ViewState["CurrentTable"] != null)
              {
                  DataTable dt = (DataTable)ViewState["CurrentTable"];
                  if (dt.Rows.Count > 0)
                  {
                      for (int i = 1; i < dt.Rows.Count; i++)
                      {
                          TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
      
      
                          box1.Text = dt.Rows[i]["Column1"].ToString();
      
                          rowIndex++;
      
                      }
                  }
              }
          }
      
          protected void ButtonAdd_Click(object sender, EventArgs e)
          {
              AddNewRowToGrid();
          }
      

      对于 aspx 代码

                                              <asp:GridView ID="Gridview1" runat="server" Style="text-align: center" ShowFooter="true" Width="70%    "
                                                  AutoGenerateColumns="false">
                                                  <Columns>
                                                      <asp:BoundField DataField="RowNumber" HeaderText="Sr No" HeaderStyle-BackColor="#99CCCC" />
                                                      <asp:TemplateField HeaderText="Coupon Code" HeaderStyle-BackColor="#99CCCC">
                                                          <ItemTemplate>
                                                              <asp:TextBox ID="TextBox1" MaxLength="10" CssClass="form-control" runat="server"></asp:TextBox>
                                                          </ItemTemplate>
      
      
                                                          <FooterStyle HorizontalAlign="Right" />
                                                          <FooterTemplate>
                                                              <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" CssClass="btn-toolbar btn" OnClick="ButtonAdd_Click" />
                                                          </FooterTemplate>
                                                      </asp:TemplateField>
                                                  </Columns>
      
                                                  <%--    <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />--%>
                                                  <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                                                  <RowStyle BackColor="White" ForeColor="#003399" />
                                                  <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                                                  <SortedAscendingCellStyle BackColor="#EDF6F6" />
                                                  <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                                                  <SortedDescendingCellStyle BackColor="#D6DFDF" />
                                                  <SortedDescendingHeaderStyle BackColor="#002876" />
                                              </asp:GridView>
      

      它只是一列,您可以浏览代码并添加几列,如果您愿意 我有一个带有删除功能的 4 列网格,检查此链接以搜索我的评论 How to delete a row using delete button for specific row in grid view?

      或者你也可以使用这个 Simple Insert Select Edit Update and Delete in ASP.Net GridView control

      【讨论】:

        猜你喜欢
        • 2013-05-24
        • 1970-01-01
        • 1970-01-01
        • 2022-08-17
        • 2011-12-13
        • 1970-01-01
        • 1970-01-01
        • 2019-09-22
        • 2017-09-26
        相关资源
        最近更新 更多