DataKey Gridview RowCommand

Last post 04-03-2009 2:14 AM by mehul.bhuva. 3 replies.

Sort Posts:

  • DataKey Gridview RowCommand

    07-01-2008, 3:10 PM
    • Member
      point Member
    • netasp2
    • Member since 07-01-2008, 6:59 PM
    • Posts 2

     I am using the Gridview_RowCommand and can get a correct index for what row it is on.  I need to grab the datakey value though for that row.  I have declared the DataKeyName in the design source view.  How do I get the datakey value.  It seems like you could code:

     gv.rows(convert.toint32(e.commandargument)).datakeys(some index for the datakey).value

    but this isn't available.  The datakeys is only available to the gridview, so how do I associate it with a row?

    I could have this primary key databound and visible in the gridview and then pull the value from the gridview, but I don't really need it to be visible.

    Any ideas?  Thanks.
  • Re: DataKey Gridview RowCommand

    07-01-2008, 3:16 PM
    Answer
    • Participant
      856 point Participant
    • stlarmon
    • Member since 06-10-2008, 9:56 PM
    • New York, USA
    • Posts 144

    the selected datakey corresponds to the selectedIndex, so if you need a specific datakey you could set the index to current row

    Gridview1.SelectedIndex = e.CommandArgument
    Dim item As String = Gridview1.SelectedDataKey(0)
     

     

    Steve
  • Re: DataKey Gridview RowCommand

    07-01-2008, 3:16 PM
    • All-Star
      86,764 point All-Star
    • ecbruck
    • Member since 12-30-2005, 7:39 PM
    • Des Moines, IA
    • Posts 9,209
    • Moderator

    object myDataKey = GridView1.DataKeys[MyRowIndex]["MyFieldName"];

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: DataKey Gridview RowCommand

    04-03-2009, 2:14 AM
    • Member
      4 point Member
    • mehul.bhuva
    • Member since 10-23-2007, 5:49 AM
    • Posts 3

     Can try this:

     

    Eg:

    if (e.CommandName == "Show")
    {
    GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
    int DemoId = Convert.ToInt32(gvdemo.DataKeys[row.RowIndex].Value);
    //You will get id here.....You code goes Here....
    }
    else if (e.CommandName == "Select1")
    {
    GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
    int DemoId = Convert.ToInt32(gvdemo.DataKeys[row.RowIndex].Value);
    //You will get id here.....You code goes Here....
    }
    }

Page 1 of 1 (4 items)