开发者

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

开发者 https://www.devze.com 2023-02-18 03:23 出处:网络
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?

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" />
  </as开发者_运维知识库p: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.


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">
0

精彩评论

暂无评论...
验证码 换一张
取 消