繁体   English   中英

如何在GridView RowCommand事件中获取SelectedDataKey值?

[英]How to get SelectedDataKey value in the GridView RowCommand Event?

我有一个这样的Gridview。

<asp:GridView runat="server" ID="gvMRLSearch" Width="100%" AutoGenerateColumns="false"
                                CssClass="datagrid" DataKeyNames="MRLID" >
<Columns>
   <asp:BoundField DataField="MRLID" HeaderText="MRL ID" Visible="false" />
   <asp:BoundField DataField="MRLCreateDate" HeaderText="MRL Create Date" />
   <asp:BoundField DataField="MRLNumber" HeaderText="MRL Number" />   
   <asp:ButtonField ButtonType="Link" CommandName="printReport" Text="Print" HeaderText="Action" />
</Columns>
</asp:GridView>

我想在GridView_RowCommand事件上获取MRLID值。 我已经这样尝试过:

protected void gvMRLSearch_RowCommand(object sender, GridViewCommandEventArgs e)
{
  try
    {
       if (e.CommandName == "printReport")
       {
         int MRLID = Convert.ToInt32(gvMRLSearch.SelectedDataKey.Value);

但是我只得到第一行MRLID,如果再次选择第二行,我得到第一行MRLID。

那是因为当您调用RowCommand事件处理程序时,它不会更改选定的网格行

设置表格的SelectedIndex属性

我得到了答案。

if (e.CommandName == "printReport")
        {
            int rowindex = Convert.ToInt32(e.CommandArgument);
            int MRLID = Convert.ToInt32(gvMRLSearch.DataKeys[rowindex].Value);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM