在GridView中获取数据绑定单元格的值

时间:2014-11-28 18:06:12

标签: c# asp.net gridview

我现在已经尝试了一段时间,似乎无法弄明白。我可以很好地提取gridview中的控件,但我需要在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获取值的简单方法?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

试试这个

您需要指定绑定字段的索引,如PurchaseOrderIDBoundField,因此其索引为0

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


    }