How to get id from GridView in GridView_RowDeleting event using C #?

I want to delete one specific row in a gridview. So I need an ID, How can I get the row id in the GridView_RowDeleting event?

Here is a list of GridView columns,

<Columns>
  <asp:TemplateField HeaderText="S.No." ControlStyle-Width="100%" ItemStyle-Width="30px">
    <ItemTemplate>
      <asp:Label ID="lblSrno" runat="server" Text=''
        <%# Container.DataItemIndex + 1 %>'></asp:Label>
    </ItemTemplate>
    <ControlStyle Width="100%" />
    <ItemStyle Width="30px" />
  </asp:TemplateField>
  <asp:TemplateField Visible="false">
    <ItemTemplate>
      <asp:Label runat="server" ID="lblDrawingID" Text=''
        <%# Bind("DrawingID")%>'></asp:Label>
    </ItemTemplate>
  </asp:TemplateField>
  <asp:BoundField DataField="CustDrawingNbr" HeaderText="Customer Drawing No." />
  <asp:TemplateField HeaderText="Status" ControlStyle-Width="100px" ItemStyle-Width="100px">
    <ItemTemplate>
      <asp:DropDownList ID="ddlStatus" runat="server" Width="100px" Enabled="false">
      </asp:DropDownList>
    </ItemTemplate>
    <ControlStyle Width="100px" />
    <ItemStyle Width="100px" />
  </asp:TemplateField>

  <asp:TemplateField Visible="false">
    <ItemTemplate>
      <asp:Label runat="server" ID="lblDrawingPGMAStatusID" Text=''
        <%# Bind("StatusID")%>'></asp:Label>
    </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField Visible="false">
    <ItemTemplate>
      <asp:Label runat="server" ID="lblDrawingPGMAID" Text=''
        <%# Bind("PGMAID")%>'></asp:Label>
    </ItemTemplate>
  </asp:TemplateField>
  <asp:CommandField ShowDeleteButton="true" HeaderText="Action" ItemStyle-HorizontalAlign="Center"
      ItemStyle-VerticalAlign="Middle">
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
  </asp:CommandField>
</Columns>

Here I populate one data file using a GridView. The columns of the DataTable are DrawingID, CustDrawingNbr, JobOrderID, StatusID, PGMAID, SeqNo. So I want to get a DrawingID when I click the Delete button.

So far, I have tried the following. But nothing gave the desired result.

  • Label lblDraw = (Label) gvApplicablePGMASearch.Rows [e.RowIndex] .Cells [2] .FindControl ("lblDrawingID"); int drawid = Convert.ToInt32 (lblDraw.Text);

  • string drawid = gvApplicablePGMASearch.DataKeys [e.RowIndex] .Value.ToString ();

  • string id = gvApplicablePGMASearch.SelectedDataKey.Value.ToString();

.

+3
1

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdeleting.aspx

, . , DataKey .

DrawingID = gvResults.DataKeys[e.RowIndex].Value

, DataKeyNames,

DrawingID = gv.DataKeys[e.RowIndex].Item("DrawingID").ToString()

, DataKeyNames gridview, .

<asp:GridView ID="gvResults" runat="server" AutoGenerateColumns="false" DataKeyNames="DrawingID">
+3

All Articles