如何使用C#从GridView_RowDeleting事件中的GridView获取ID?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何使用C#从GridView_RowDeleting事件中的GridView获取ID?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2523字,纯文字阅读大概需要4分钟。
内容图文
我想删除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填充一个数据表.
数据表列是DrawingID,CustDrawingNbr,JobOrderID,StatusID,PGMAID,SeqNo.
因此,当我单击“删除”按钮时,我想获取DrawingID.
至今,
我尝试了以下方法.但是没有任何东西能达到预期的效果.
Label lblDraw = (Label)gvApplicablePGMASearch.Rows[e.RowIndex].Cells[2].FindControl(“lblDrawingID”);
int drawid = Convert.ToInt32(lblDraw.Text);string drawid = gvApplicablePGMASearch.DataKeys[e.RowIndex].Value.ToString();
- string 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
编辑,如果您有多个DataKeyNames则将是这样
DrawingID = gv.DataKeys[e.RowIndex].Item("DrawingID").ToString()
只需确保已在gridview上设置了DataKeyNames即可,这应该可以工作.
<asp:GridView ID="gvResults" runat="server" AutoGenerateColumns="false" DataKeyNames="DrawingID">
内容总结
以上是互联网集市为您收集整理的如何使用C#从GridView_RowDeleting事件中的GridView获取ID?全部内容,希望文章能够帮你解决如何使用C#从GridView_RowDeleting事件中的GridView获取ID?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。