【问题标题】:How to add rows dynamically to the GridView based on the TextBox value?如何根据 TextBox 值动态向 GridView 添加行?
【发布时间】:2012-02-08 12:57:03
【问题描述】:

我有一个 TextBox、GridView 和 Button,例如“txtCount”、“gvCount”和“btnCount”。

gvCount 有以下列

<Columns>
      <asp:TemplateField Visible="false">
      <ItemTemplate>
      <asp:Label ID="lblRTDetailID" runat="server" Text='<%# Bind("RTDetailID") %>'></asp:Label>
      </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="Assy. No.">
      <ItemTemplate>
      <asp:TextBox ID="txtgvAssyNo" runat="server" Text='<%# Bind("AssyNo") %>'></asp:TextBox>
      </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="Position">
      <ItemTemplate>
      <asp:DropDownList ID="ddlgvPos" runat="server" Text='<%# Bind("Position") %>'>
      </asp:DropDownList>
      </ItemTemplate>
      </asp:TemplateField>
</Columns>

如果文本框“txtCount”包含文本“10”。单击按钮“btnCount”时,将向 GridView 添加 10 行。同样,我需要根据 TextBox 值向 GridView 添加行。如何做到这一点?

【问题讨论】:

  • 那 10 行中的值应该是什么?
  • @Magnus:所有 TextBox 值都应该为空,并且 DropDownlist 应该显示 SelectedValue = "-1";

标签: c# asp.net visual-studio-2008 gridview


【解决方案1】:
gv.DataSource = Enumerable.Range(0, int.Parse(txtCount.Value))
               .Select (e => new 
               { 
                  RTDetailID = "", 
                  AssyNo = "", 
                  Position = "-1"
               });
gv.DataBind();

【讨论】:

    【解决方案2】:

    如果我理解,您想在您的代码隐藏中向 gridview 添加新行吗?

    获取 TextBox 值,转换为 int,并使用一个通用循环使用它:http://www.stellarpc.com/articles/board.aspx?id=33

    【讨论】:

      【解决方案3】:

      您始终可以使用数据表。使用从 0 开始到文本框的 text-1 的 for 循环将数据行添加到数据表中。然后将数据表设置为 Gridview 数据源。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-11
        • 1970-01-01
        • 2020-10-17
        • 1970-01-01
        • 2010-09-19
        • 1970-01-01
        • 2012-10-30
        相关资源
        最近更新 更多