获取网格视图中数据绑定单元格的值

本文关键字:单元格 数据绑定 网格 视图 获取 | 更新日期: 2023-09-27 18:32:20

我已经尝试了一段时间了,似乎无法弄清楚。我可以很好地提取网格视图中的控件,但我需要在 GridView 中获取数据绑定单元格的值,并且似乎无法找到方法。这是我的代码:

网页:

 <asp:GridView ID="ReceiverPanel" runat="server" AutoGenerateColumns="False" DataSourceID="ODSPopulatePOItemList">
    <Columns>
        <asp:BoundField DataField="PurchaseOrderID" HeaderText="PurchaseOrderID" SortExpression="PurchaseOrderID" Visible="False" />
        <asp:BoundField DataField="StockItemID" HeaderText="StockItemID" SortExpression="StockItemID" />
        <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
        <asp:BoundField DataField="QuantityOnOrder" HeaderText="QuantityOnOrder" SortExpression="QuantityOnOrder" />
        <asp:BoundField DataField="QuantityOutstanding" HeaderText="Outstanding" SortExpression="QuantityOutstanding" />
        <asp:TemplateField HeaderText="Receive">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="Received" Text='<%# Eval("Received") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Return">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="Returned" Text='<%# Eval("ReturnedQuantity") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Reason">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="Reason" Text='<%# Eval("Reason") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码隐藏:

protected void ReceiveButton_Click(object sender, EventArgs e)
{
    // Gather data from gridview
    foreach (GridViewRow row in ReceiverPanel.Rows)
    {
        // Find controls in the Gridview
        //var purchaseOrderIdCtrl = row.FindControl("PurchaseOrderID") as HiddenField;
        //var stockItemCtrl = row.FindControl("")
        var receivedCtrl = row.FindControl("Received") as TextBox;
        var returnedCtrl = row.FindControl("Returned") as TextBox;
        var reasonCtrl = row.FindControl("Reason") as TextBox;
        //SELECT DATABOUND CONTROL var stockItemNo = row.SelectedRow.Cells[2].Text ;
        //int stockItemNum = int.Parse(stockItemNo);
        int received = int.Parse(receivedCtrl.Text);
        int returned = int.Parse(returnedCtrl.Text);
        string reason = reasonCtrl.Text;
        Update_StockItem(stockItemNum, received);
    }

有没有一种简单的方法可以从 GridView 中获取值?任何帮助将不胜感激。

获取网格视图中数据绑定单元格的值

尝试这样做

您需要指定绑定字段的索引,例如首先BoundField PurchaseOrderID,因此其索引0

 foreach (GridViewRow row in ReceiverPanel.Rows)
    {
         string PurchaseOrderID =row.Cells[0].Text

    }