我想删除 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>
Run Code Online (Sandbox Code Playgroud)
这里我用这个 GridView 填充一个数据表。数据表列包括 DrawingID、CustDrawingNbr、JobOrderID、StatusID、PGMAID、SeqNo。所以我想在单击“删除”按钮时获取 DrawingID。
到目前为止,我已经尝试过以下几件事。但一切都没有给出预期的结果。
标签 lblDraw = (Label)gvApplicablePGMASearch.Rows[e.RowIndex].Cells[2].FindControl("lblDrawingID"); int drawid = Convert.ToInt32(lblDraw.Text);
字符串drawid = gvApplicablePGMASearch.DataKeys[e.RowIndex].Value.ToString();
- 字符串 id = gvApplicablePGMASearch.SelectedDataKey.Value.ToString();
但以上都没有给出想要的结果。
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdeleting.aspx
您可以确定被单击的行的索引。获得该索引后,访问该项目的 DataKey。
DrawingID = gvResults.DataKeys[e.RowIndex].Value
Run Code Online (Sandbox Code Playgroud)
编辑如果您有多个 DataKeyNames 那么它会像这样
DrawingID = gv.DataKeys[e.RowIndex].Item("DrawingID").ToString()
Run Code Online (Sandbox Code Playgroud)
只需确保您已在 gridview 上设置了 DataKeyNames,这应该可以工作。
<asp:GridView ID="gvResults" runat="server" AutoGenerateColumns="false" DataKeyNames="DrawingID">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29704 次 |
| 最近记录: |