使用asp网格视图数据填充DataTable

时间:2018-05-25 06:54:55

标签: asp.net gridview datatable

我有一个带有文本框的网格视图。

    <asp:GridView ID="AgriGrid"
runat="server"
AutoGenerateColumns="false">
<Columns>
    <asp:TemplateField HeaderText="S. No.">
        <ItemTemplate>
            <asp:Label ID="lblagrigridsn" runat="server" Text='<%# Bind("sn") %>' />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="CROPS">
        <ItemTemplate>
            <asp:Label ID="lblcropsagrigrid" runat="server" Text='<%# Bind("crops") %>' />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="PRODUCTION">
        <ItemTemplate>
            <input type="number" min="0" value="0" id="txtAgriGridProduction" class="form-control" onkeypress="return numberOnly(event)" runat="server" autocomplete="off">
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="CULTIVATED LAND">
        <ItemTemplate>
            <asp:TextBox ID="txtAgriGridCultivatedLand" Width="80px" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="CULTIVATED LAND UNIT">
        <ItemTemplate>
            <asp:TextBox ID="txtAgriGridCultivatedUnit" Width="80px" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="SALES">
        <ItemTemplate>
            <input type="number" min="0" value="0" class="form-control" onkeypress="return numberOnly(event)" runat="server" id="txtAgriGridSell" autocomplete="off">
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

用户在网格文本框txtAgriGridProduction中填写生产数量。按下提交按钮我试图将asp网格数据插入DataTable。但我得到的是空值。

提交按钮代码块

 DataTable agricrops_dt = new DataTable();
agricrops_dt.Columns.Add(new DataColumn("CROPS", typeof(string)));
agricrops_dt.Columns.Add(new DataColumn("PRODUCTION", typeof(int)));
DataRow drow = null;
foreach (GridViewRow gr in AgriGrid.Rows)
{
    drow = agricrops_dt.NewRow();
    drow["CROPS"] = gr.Cells[1].Text;
    drow["PRODUCTION"] = gr.Cells[2].Text;
    agricrops_dt.Rows.Add(drow);
}

但我得到的是空值。为什么会这样?

2 个答案:

答案 0 :(得分:1)

HTML标记

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:TemplateField HeaderText="S.No.">
                                <ItemTemplate>
                                    <asp:Label ID="lblagrigridsn" runat="server" Text='<%# Bind("sn") %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="CROPS">
                                <ItemTemplate>
                                    <asp:Label ID="lblcropsagrigrid" runat="server" Text='<%# Bind("crop") %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="PRODUCTION">
                                <ItemTemplate>
                                    <input type="text" min="0" value="0" class="form-control" onkeypress="return numberOnly(event)" runat="server" id="txtAgriGridProduction" autocomplete="off" />
                                </ItemTemplate>
                            </asp:TemplateField>
        </Columns>

    </asp:GridView>
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Crop]"></asp:SqlDataSource>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="Button" />

按钮上的方法背后的代码单击

protected void Button1_Click1(object sender, EventArgs e)
{
    DataTable agricrops_dt = new DataTable();
    agricrops_dt.Columns.Add(new DataColumn("CROPS", typeof(string)));
    agricrops_dt.Columns.Add(new DataColumn("PRODUCTION", typeof(int)));
    DataRow drow = null;
    foreach (GridViewRow gr in GridView1.Rows)
    {
        drow = agricrops_dt.NewRow();
        drow["CROPS"] = ((Label)gr.FindControl("lblcropsagrigrid")).Text;
        drow["PRODUCTION"] = ((HtmlInputControl)gr.FindControl("txtAgriGridProduction")).Value;
        agricrops_dt.Rows.Add(drow);
    }


}

您的数据存储在数据表中。

答案 1 :(得分:1)

通过检查您的代码,我们可以说将asp:TextBox更改为input type="text"然后生成

drow["CULTIVATED_LAND"] = ((HtmlInputControl)gr.FindControl("txtAgriGridCultivatedLand")).Value;
drow["CULTIVATED_LAND_UNIT"] = ((HtmlInputControl)gr.FindControl("txtAgriGridCultivatedUnit")).Value;

但是如果你想使用asp:TextBox,你必须使用

  drow["CULTIVATED_LAND"] =  ((TextBox)gr.FindControl("txtAgriGridCultivatedLand")).Text;
  drow["CULTIVATED_LAND_UNIT"] =  ((TextBox)gr.FindControl("txtAgriGridCultivatedUnit")).Text;

希望它有效。