开发者

How to get SelectedDataKey value in the GridView RowCommand Event?

I have one Gridview like this.

开发者_JS百科
<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>

I want to Get the MRLID value on the GridView_RowCommand Event. I have tried like this:

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

But I get only the first row MRLID only, if selects the second again I get the first row MRLID.


That is because when you call RowCommand event handler , it will not change the selected row of grid

set SelectedIndex property of the grid


I got the answer.

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜