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

Rate It (4)

Last post 12-31-2008 4:59 AM by sirdneo. 26 replies.

Sort Posts:

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

    06-19-2007, 12:45 PM
    • Member
      66 point Member
    • seed
    • Member since 05-21-2007, 11:32 AM
    • Posts 46

    I'll answer my own question.  FINAL ANSWER!

     You need to set the rowindex on the rowdatabound event.

     

    Protected Sub gvResults_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvResults.RowDataBound

        btnDocNbr.CommandArgument = e.Row.RowIndex.ToString

    End Sub

     Then you can access the CommandArgument in the RowCommand

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

    ' Convert the row index stored in the CommandArgument

    ' property to an Integer.

    Dim index As Integer = Convert.ToInt32(e.CommandArgument)

    ' Retrieve the row that contains the button clicked

    ' by the user from the Rows collection.

    Dim row As GridViewRow = gvResults.Rows(index)

    End Sub 

     

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

    07-17-2007, 7:12 PM
    • Member
      2 point Member
    • roninou
    • Member since 07-05-2007, 8:32 AM
    • Posts 4

     

    Hi i am doing the exact same thing. I retrieve a non primary key from the datakeys. I works great

     but

    when i implement an update method to the gridview i have an issue.

    Well the column i have put in the datakeys collection is added to the parameters  passed to the object_datasource update function as "original_NAME OF THE COLUMN"

     and when i click on the updated button, i get an error

     
    Object of type 'System.DBNull' cannot be converted to type 'System.Int32'.

     

    it looks like thoses original_... values are not set properly.

     

    can someone help me please  ?

     

    thanks

     

    Dan

     

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

    10-02-2007, 11:54 AM
    StrongTypes:

    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.

     
    I understand the code you provided, but what event do I attach it to?

    THanks.

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

    10-16-2007, 11:17 AM
    • Member
      57 point Member
    • Sleb
    • Member since 02-19-2007, 8:31 PM
    • Posts 55

     Check this article, it resumes this post quite well. http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.buttonfield.aspx

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

    11-01-2007, 9:58 AM
    • Member
      457 point Member
    • ks2007
    • Member since 09-24-2007, 9:14 PM
    • Posts 294

    Is the above code to be put in the GridView1_RowCommand method or in someother method? Also, would the same code apply when we have paging and the the row selected happens to be, say, row 4 on page 3? If, possible could you please illustrate the code? I hope you will not mind my asking so many questions as I am a newbie. Thanks.

    If this answered your question, be sure to mark it as the answer.
  • Re: How do I retrieve a non-primary DataKey value from a GridView?

    01-02-2008, 5:11 AM
    • Member
      2 point Member
    • louiechung
    • Member since 01-02-2008, 10:06 AM
    • Posts 5

    Thanks, I just need this procedure.  One point to note is that I need to add the non-primarykey field in datakeynames. 

    Dim MyKey as integer = CType(grd.SelectedDataKey(0), Integer)
    Dim index As Integer = grd.SelectedIndex
    Dim AnotherField as string = grd.DataKeys(index).Item(
    "AnotherFieldName").ToString()
     

    Also, in your aspx file, remember to add this:
    DataKeyNames="PrimaryKeyField,AnotherFieldName"

     

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

    03-07-2008, 3:04 AM
    • Member
      4 point Member
    • N1ck73
    • Member since 03-05-2008, 4:50 PM
    • Posts 2

    See this post http://forums.asp.net/p/1168036/2214253.aspx#2214253

     or if you cant be bothered to click, read this:

    ---- 

    CommandArgument='<%# GridView1.Rows.Count.ToString() %>' 

     I like MSchumacher´s solution for this and I have used the same method, previously I achieved it using the RowDataBound event

    protected void _grid_RowDataBound(object sender, GridViewRowEventsArgs e) {

            if (e.Row.RowType == DataControlRowType.DataRow) {

                  //Button button = e.Row.Cells[0].FindControls("btnEdit") as ImageButton;

                 button.CommandArgument = e.Row.RowIndex.ToString()

          }

    }

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

    08-09-2008, 1:05 AM
    • Member
      10 point Member
    • lotusisoreg
    • Member since 06-26-2008, 3:50 PM
    • Posts 15

    And how is the selectParameter for the SQLdatasource will be?

     For the single data key is like this :

    <asp:ControlParameter ControlID="GridView1" Name="OrderID" PropertyName="SelectedValue" Type="String" />

    How is the the 2 data keys will be?

     

     

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

    08-19-2008, 9:48 AM
    I am not sure how to do it
  • Re: How do I retrieve a non-primary DataKey value from a GridView?

    12-13-2008, 7:17 AM
    • Contributor
      2,189 point Contributor
    • davidfowl
    • Member since 08-17-2008, 9:50 PM
    • Redmond
    • Posts 450
    • AspNetTeam
      Moderator

    DataKeyNames can be anything it doesn't have to be a primary key. The fact that it is most of the time is a convention. You can put any amount of fields in DataKeyNames and access the values using:

    GridView.DataKeys[rowIndex].Values[fieldName]

    OR

    GridView.DataKeys[rowIndex].Values[fieldIndex]

    Checkout my blog post on extracting values from data controls.

    David Fowler
    SDE, ASP.NET Team, Microsoft
  • Re: How do I retrieve a non-primary DataKey value from a GridView?

    12-31-2008, 4:35 AM
    • Member
      123 point Member
    • Johncy
    • Member since 09-20-2008, 6:19 AM
    • Allappuzha
    • Posts 114

    Its just simple

    just use this 

    GridView.DataKeys[Rowindex].Values[coloumn index]

    here the row index and coloumn index are jus two names. you have to give the value of row index and colomn index

    this works in C#

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

    12-31-2008, 4:59 AM
    • Contributor
      7,266 point Contributor
    • sirdneo
    • Member since 12-16-2008, 12:45 AM
    • Karachi, Pakistan
    • Posts 1,141

     You can retrive non key value from e.Row.DataItem in/after grid databound event.

     

    Thanks,
    Zeeshan Umar

    ~Please Mark As Answer, if one or multiple posts, which helped you in your problem. So that it might be useful for others~

    My Blog
Page 2 of 2 (27 items) < Previous 1 2