Get DataKey on RowDataBound

Discussion in 'ASP .Net' started by MasterChief, Feb 15, 2006.

  1. MasterChief

    MasterChief Guest

    I am trying to add an attribute to a gridview row so when you click on
    it, it will pop up an alert saying the current datakey for that row.
    How is it possible to get a datakey when using the rowdatabound I want
    to do something like

    If (e.Row.RowType = DataControlRowType.DataRow) Then
    e.row.attribute.add("onClick", "alert('" + KeyID + "')'")
    End If

    but I don't know how to get the KeyID? Any help would be appreciated.
     
    MasterChief, Feb 15, 2006
    #1
    1. Advertising

  2. MasterChief

    tdavisjr Guest

    MasterChief wrote:
    > I am trying to add an attribute to a gridview row so when you click on
    > it, it will pop up an alert saying the current datakey for that row.
    > How is it possible to get a datakey when using the rowdatabound I want
    > to do something like
    >
    > If (e.Row.RowType = DataControlRowType.DataRow) Then
    > e.row.attribute.add("onClick", "alert('" + KeyID + "')'")
    > End If
    >
    > but I don't know how to get the KeyID? Any help would be appreciated.


    You need to hande the RowCommand Event.

    Then retreive the datakey

    DataKey key = GridView1.DataKeys[e.CommandArgument]

    Then the the key should have a Value property which is the datakey.
    Now, I'm doing this totally from memory; but this should point you in
    the right diretion.
     
    tdavisjr, Feb 15, 2006
    #2
    1. Advertising

  3. MasterChief

    tdavisjr Guest

    Oops. The index to the DataKeys is probably an integer so please cast
    c.CommandArgument appriately.
     
    tdavisjr, Feb 15, 2006
    #3
  4. MasterChief

    MasterChief Guest

    I can get the DataKey using the RowCommand Event but I want to be able
    to put the OnClick attribute into the Row and have it alert the KeyID.
    When I try to get the value of KeyID that I got in RowCommand it comes
    back with a null value. The only way I can find to add an attribute to
    a row is during the RowDataBound Event.

    Public Class _MasterTestBed
    Inherits System.Web.UI.Page
    Dim KeyID As String
    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e
    As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
    GridView1.RowCommand
    KeyID = GridView1.DataKeys(e.CommandArgument).Value.ToString
    End Sub

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal
    e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    GridView1.RowDataBound
    If (e.Row.RowType = DataControlRowType.DataRow) Then
    e.Row.Attributes.Add("onMouseOver",
    "this.style.backgroundColor='lightgrey'")
    e.Row.Attributes.Add("onMouseOut",
    "this.style.backgroundColor='Transparent'")
    e.Row.Attributes.Add("onClick", "alert('" + KeyID + "')'")
    End If
    End Sub
    End Class
     
    MasterChief, Feb 15, 2006
    #4
  5. MasterChief

    shuameston

    Joined:
    Dec 29, 2007
    Messages:
    1
    I realize this thread is almost 2 years old, but I had the same question today. I searched extensively and never found an answer. When I searched for "gridview rowdatabound datakey" on Google, this is the first link given. I finally figured out how to do it, so I thought I'd post what I figured out here in case somebody else is having the same problem I was.

    The task I was trying to perform was to fill another gridview in each row based on the datakey of that row. To use the datakey for something else, you'll have to change the code a little. Also, I am a C# guy. I used a converter (http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx) to get the vb code, so I'm not positive the syntax is correct, but it should be pretty close. When I use the same converter to go from VB to C#, it usually works.

    The code below works when there is one datakey in the GridView. I didn't test to see what would happen if more datakeys were set in the gridview. I also didn't figure out how to get other datakeys if there are more than one. This is for if you have one datakey, and you want to get the datakey of the current row during rowdatabound:

    VB:
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
    DirectCast(e.Row.FindControl("SqlDataSource1"), SqlDataSource).SelectParameters("primaryKey").DefaultValue = DirectCast(sender, GridView).DataKeys(e.Row.RowIndex).Value.ToString()
    End If
    End Sub

    C#:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    ((SqlDataSource)e.Row.FindControl("SqlDataSource1")).SelectParameters["primaryKey"].DefaultValue = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
    }
    }
     
    shuameston, Dec 29, 2007
    #5
  6. MasterChief

    Mobeen

    Joined:
    Nov 19, 2008
    Messages:
    1
    Accessing Multiple Datakeys in RowDataBound method

    protected void GridView2_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
    string local_idcode="";
    string local_year="";
    if (e.Row.RowType == DataControlRowType.DataRow)
    {


    local_idcode = Convert.ToString(GridView2.DataKeys[e.Row.RowIndex].Values[1].ToString());
    local_year = Convert.ToString(GridView2.DataKeys[e.Row.RowIndex].Values[2].ToString());

    }

    Following are the statement of interest
    Convert.ToString(GridView2.DataKeys[e.Row.RowIndex].Values[1].ToString());

    Convert.ToString(GridView2.DataKeys[e.Row.RowIndex].Values[2].ToString());

    In Values[1] and Values[2] 1 & 2 shows the position of the DataKeys in you Gridview. Values Index start from [0].

    Also don't forget to add if condition
    if (e.Row.RowType == DataControlRowType.DataRow)
    without that you may get an index error.

    Mobeen.
     
    Mobeen, Nov 19, 2008
    #6
  7. MasterChief

    fragiletruce

    Joined:
    Dec 29, 2008
    Messages:
    1
    Get datakey at gridview rowdatabound event

    Dim s As String = DirectCast(sender, GridView).DataKeys(e.Row.RowIndex).Value.ToString()
     
    fragiletruce, Dec 29, 2008
    #7
  8. MasterChief

    siddersscfc

    Joined:
    Aug 9, 2011
    Messages:
    1
    I know this an old post but!!

    This helped me out of a pickle :congrats:
     
    siddersscfc, Aug 9, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Sutorius

    get datakey from datalist

    Andy Sutorius, Mar 3, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    7,273
    Karl Seguin
    Mar 4, 2005
  2. mike
    Replies:
    0
    Views:
    5,392
  3. mark4asp
    Replies:
    2
    Views:
    2,981
    meerhassan
    Oct 28, 2010
  4. jc
    Replies:
    1
    Views:
    1,179
    Teresita Núñez - SorCereSs -
    Mar 6, 2008
  5. David C

    Get DataKey value in GridView

    David C, Dec 2, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    11,072
    David C
    Dec 2, 2008
Loading...

Share This Page