Another way to do this in order to avoid accessing GridView cells by index is to convert the desired BoundField to TemplateField in the constructor. Then use the FindControl method to get the text value in this cell. By default, the designer will provide the Label control with a new TemplateField name of type "Label1".
, , . , aspx , :
<asp:BoundField DataField="PurchaseOrderID" ...</asp:BoundField>
<asp:TemplateField HeaderText="PurchaseOrderID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("PurchaseOrderID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PurchaseOrderID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
:
string strPrimaryid = gvr.FindControl("Label1").Text;
, , - DataKeys GridView. DataKeyNames GridView:
<asp:GridView ID="gvOrders" runat="server" DataKeyNames="PurchaseOrderID" ...>
, PurchaseOrderID int, :
int primaryid = (int)gvOrders.DataKeys[gvr.RowIndex].Value;