3

I want to delete one particular row in Gridview. So I need ID, How can I get the ID of the row in GridView_RowDeleting Event?

Here is the column list of the GridView,

<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 fill one datatable with this GridView. The DataTable Columns are DrawingID, CustDrawingNbr, JobOrderID, StatusID, PGMAID, SeqNo. So I want to get the DrawingID when I click the Delete button.

So far, I have tried the following things. But nothing has given the desired result.

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

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

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

But none of the above has not given the desired result.

3
  • It seems to me that id should come with 'Delete' event.. Mar 18, 2011 at 11:10
  • @Bugai. I cant get u. Ellaborate please.
    – thevan
    Mar 18, 2011 at 11:15
  • Why are you using all those Labels inside TemplateFields, why not just use a asp:BoundField and set the DataField? And if you're just using them for hidden ID's, you might prefer instead to just list the ID's in the DataKeyNames, e.g. DataKeyNames="DrawingID,StatusID,PGMAID".
    – bgs264
    Mar 18, 2011 at 11:20

1 Answer 1

5

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

You can determine the index of the row that was clicked. Once you've got that index, access the DataKey for that item.

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

EDIT if you have multiple DataKeyNames then it would be like this

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

Just make sure you've set the DataKeyNames set on the gridview and this should work.

<asp:GridView ID="gvResults" runat="server" AutoGenerateColumns="false" DataKeyNames="DrawingID">
2
  • @NickG You're probably right, this is a nearly-six-year-old answer and I was probably writing VB then haha.
    – bgs264
    Feb 15, 2017 at 19:33
  • Well the comment you're replying to is 2 years old :)
    – NickG
    Feb 16, 2017 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.