如何在C#中使用GridView_RowDeleting事件从GridView中获取ID?

3

我希望删除GridView中的一行。因此,我需要ID,如何在GridView_RowDeleting事件中获取该行的ID?

以下是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>

这里我使用GridView填充了一个DataTable。DataTable的列是DrawingID、CustDrawingNbr、JobOrderID、StatusID、PGMAID和SeqNo。因此,我想在单击删除按钮时获取DrawingID。
到目前为止, 我尝试了以下几件事情。但是没有任何一件事情能够得到所需的结果。
  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();
但是上述所有方法都未能得到所需的结果。

我认为id应该带有“删除”事件。 - Andrew Orsich
@Bugai。我不明白你的意思,请详细说明一下。 - thevan
为什么在TemplateFields中使用那么多标签,而不是只使用asp:BoundField并设置DataField呢?如果您只是用它们来隐藏ID,那么您可能更喜欢在DataKeyNames中列出这些ID,例如DataKeyNames="DrawingID,StatusID,PGMAID"。 - bgs264
1个回答

5
你可以确定被点击的行的索引。一旦你获得了该索引,就可以访问该项的DataKey。请参考:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdeleting.aspx
DrawingID = gvResults.DataKeys[e.RowIndex].Value

编辑 如果您有多个DataKeyNames,则应该像这样

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

请确保在GridView上设置了DataKeyNames,这样就可以正常工作。

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

@NickG 你可能是对的,这个回答已经快六年了,当时我可能还在写VB哈哈。 - bgs264
好的,你回复的评论已经有两年了 :) - NickG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接