【问题标题】:How to get ID from the GridView in GridView_RowDeleting event using C#?如何使用 C# 从 GridView_RowDeleting 事件中的 GridView 获取 ID?
【发布时间】:2011-07-18 02:07:24
【问题描述】:

我想删除 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 列是 DrawingID、CustDrawingNbr、JobOrderID、StatusID、PGMAID、SeqNo。 所以我想在单击删除按钮时获取 DrawingID。

到目前为止, 我尝试了以下事情。但是什么都没有给出预期的结果。

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

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

  3. 字符串 id = gvApplicablePGMASearch.SelectedDataKey.Value.ToString();

但以上都没有给出想要的结果。

【问题讨论】:

  • 在我看来 id 应该带有“删除”事件..
  • @Bugai。我不能得到你。请详细说明。
  • 你为什么在 TemplateFields 中使用所有这些标签,为什么不使用 asp:BoundField 并设置 DataField?如果您只是将它们用于隐藏 ID,您可能更愿意只在 DataKeyNames 中列出 ID,例如DataKeyNames="DrawingID,StatusID,PGMAID".

标签: c# asp.net


【解决方案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()

只要确保您已经在 gridview 上设置了 DataKeyNames 就可以了。

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

【讨论】:

  • @NickG 你可能是对的,这是一个有将近六年历史的答案,那时我可能正在写 VB,哈哈。
  • 嗯,你回复的评论是 2 岁 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-21
相关资源
最近更新 更多