How do I retrieve a non-primary DataKey value from a GridView?

Rate It (3)

Last post 03-07-2008 3:04 AM by N1ck73. 21 replies.

Sort Posts:

  • How do I retrieve a non-primary DataKey value from a GridView?

    01-06-2006, 2:03 PM

    The GridView control has the functionality to allow multiple DataKey values to be assigned to it using the DataKeyNames property. However, the method of retrieving a DataKey that is not the primary key is, for the most part, undocumented. The Values property will allow you to get any DataKey value.

    VB.NET
    GridView.DataKeys(Index1).Values(Index2)

    C#
    GridView.DataKeys[Index1].Values[Index2]

    Where GridView is the ID of the GridView control, Index1 is the index of the current row in the GridView, and Index2 is the name of the DataKey.

    Ryan Olshan
    Microsoft MVP, ASP.NET
    Blog | Group | Website | Strong Coders Community

    How to ask a question
  • Re: How do I retrieve a non-primary DataKey value from a GridView?

    01-07-2006, 2:29 PM
    • Loading...
    • sbyard
    • Joined on 03-26-2003, 3:42 AM
    • North Yorkshire - England
    • Posts 1,172

    I haven't looked at the documentation in detail or built anything with mukiple keys, but a quick glance at the intellisense for data key names indicates that this at least relates to the primary key.

    This leads me to think that this whole area relates to complex (primary) keys, rather than other indexes on the data.

    E.g. a child data table might allow only one "name" value per parent, so the primary key is the foreign key value from the parent, plus a unique "name" value in the child table.

    If it was easy, everybody would be doing it.
  • Re: How do I retrieve a non-primary DataKey value from a GridView?

    01-27-2006, 2:20 PM
    • Loading...
    • docluv
    • Joined on 06-29-2002, 11:16 PM
    • Willow Spring NC
    • Posts 1,311

    Man I thought I knew this one, but I did not.  I am not sure where you need to retrieve the dataitem for the row, but for the RowDataBound event it would look something like this:

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

       Dim drv As [String] = CType(e.Row.DataItem, [System.String])

      'Perfom some logic on the drv here

     

    end sub

    I use string there as an arbitrary object value since I took it from some code where the data item is a string, it could be a datarow, custom object, etc.

    In response to a RowCommand event handler I could not find a way to intuitively get the row index the wevent was fired from.  In this case you can either attach the index as the commandargument or retrieve it based on the commandargument of your event firer, like a link button.

    Until I can see a better way this may be what you have to do.

  • Re: How do I retrieve a non - primary Data Key value from a Grid View????

    02-01-2006, 1:31 AM
    How do I retrieve a non - primary Data Key value from a Grid View????
    Vishal
  • Re: How do I retrieve a non - primary Data Key value from a Grid View????

    02-01-2006, 8:30 AM

    vishalsharma wrote:
    How do I retrieve a non - primary Data Key value from a Grid View????

    The answer is in the first thread to this post.

    Ryan Olshan
    Microsoft MVP, ASP.NET
    Blog | Group | Website | Strong Coders Community

    How to ask a question
  • Re: How do I retrieve a non-primary DataKey value from a GridView?

    06-10-2006, 12:37 AM
    • Loading...
    • caraurelio
    • Joined on 11-19-2003, 2:58 PM
    • Posts 1
    Dim index As Integer = CInt(e.CommandArgument)
    Me.GridView.DataKeys(index).Value)
    --
    Carlos Angulo
    MCP - MCAD
  • Re: How do I retrieve a non-primary DataKey value from a GridView?

    09-05-2006, 1:14 PM
    • Loading...
    • ATrien
    • Joined on 09-05-2006, 5:11 PM
    • Posts 1

    This syntax works best for me:

     Dim index as integer = e.Row.DataItemIndex

    intKey1 = GridView.DataKeys(index).Item(0)

    strKey2 = GridView.DataKeys(index).Item(1)

     

     

     

    ForTheWin
  • Re: How do I retrieve a non-primary DataKey value from a GridView?

    01-19-2007, 4:56 PM
    • Loading...
    • ploki
    • Joined on 01-19-2007, 9:42 PM
    • Posts 22

    Hello,

    This is a very intersting topic as I am working on a project that involves a table of items in which members of that table have relationships to other names in the same table. What would suggest is the best method of dealing with parent-sibling relationships within one table?

    I am new to asp but I have my database-dal-bll thing going and need to deal with it programaticaly.

    Thanks for the topic!

  • Re: How do I retrieve a non-primary DataKey value from a GridView?

    01-19-2007, 4:59 PM
    • Loading...
    • ploki
    • Joined on 01-19-2007, 9:42 PM
    • Posts 22
    Oh I meant Parent - Child - Sibling - friend - lover .................... 
  • Re: How do I retrieve a non-primary DataKey value from a GridView?

    01-19-2007, 5:08 PM
    • Loading...
    • ploki
    • Joined on 01-19-2007, 9:42 PM
    • Posts 22

    Lets be a bit more clear

    1, john, motherId, fatherId

    2, fredy, motherId, fatherId

    3, mary, motherId, fatherId

    etc.....

    all relationships within one table...

  • Re: How do I retrieve a non-primary DataKey value from a GridView?

    02-19-2007, 3:20 PM
    • Loading...
    • sestyd
    • Joined on 05-18-2006, 7:21 PM
    • Posts 9

    Ryan,

    Could you please take a minute to elaborate for a newbie? I am having this exact same problem, but I need a little more help than just this line of code please. I'm not sure how to implement this code. Should this be written in the GridView_SelectedIndexChanged event? And then what? How does this second parameter get passed along to the SqlDataSource?

    Thanks

     

     

     

  • Re: How do I retrieve a non-primary DataKey value from a GridView?

    02-22-2007, 10:37 AM

     

    Nice topic and well explained. This would be helpful to a lot of readers...

    Joydip Kanjilal
    Microsoft Most Valuable Professional (ASP.NET)
    http://aspadvice.com/blogs/joydip
  • Re: How do I retrieve a non-primary DataKey value from a GridView?

    05-21-2007, 7:38 AM
    • Loading...
    • seed
    • Joined on 05-21-2007, 11:32 AM
    • Posts 19

     Okay, am I missing something?  When I try to get the (index1) using the CommandArgument it comes back error converting to Cint. 

    Protected Sub gvResults_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvResults.RowCommand

    Server.Transfer("~/myPage.aspx?key=" & gvResults.DataKeys(CInt(e.CommandArgument)).Item(0).toString)

    End Sub

    PS.  I have option strict and option explicit  set to 'on'

  • Re: How do I retrieve a non-primary DataKey value from a GridView?

    05-21-2007, 9:44 AM
    • Loading...
    • seed
    • Joined on 05-21-2007, 11:32 AM
    • Posts 19

    I found my mistake and figured I'd put it here just in case someone else might benefit.  I forgot 2 things on my asp:datagrid form.

     1.

    DataKeyNames="MyKey" in the <asp:gridview>

    <asp:GridView ID="gvResults" EmptyDataText="No records found" runat="server" AutoGenerateColumns="false" CssClass="Grid" Width="100%" AllowPaging="True" AllowSorting="True" DataKeyNames="MyKey">

     2.

    CommandArgument="0"  in my <asp:linkbutton>

    <asp:linkbutton ID="btnFieldHdr" ForeColor="white" runat="server" Text="Name" CommandArgument="0" />

     Hope this helps someone!

     

     

  • Re: How do I retrieve a non-primary DataKey value from a GridView?

    06-19-2007, 10:02 AM
    • Loading...
    • seed
    • Joined on 05-21-2007, 11:32 AM
    • Posts 19

    Or so I thought.  What I didn't realize is what the CommandArgument really could be used for.  It did me no good to set it to 0 (zero).  I need to use it to get the index of the row since I'm using GridViewCommandEventsArg.  So back to one of the first posts on this topic.

    How do I get the selected row index using GridViewCommandEventArgs.  Initially it was said that you can't use e.row so I need to either have the button CommandArgument send it or get it some other way. 

    Any Help?

    CommandArgument="SOME CODE??"  or a Better solution to have it in my

    Protected Sub gvResults_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvResults.RowCommand

    End Sub

     

Page 1 of 2 (22 items) 1 2 Next >