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:

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

    06-19-2007, 12:45 PM
    • Loading...
    • seed
    • Joined on 05-21-2007, 11:32 AM
    • Posts 19

    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
    • Loading...
    • roninou
    • Joined on 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
    • Loading...
    • Sleb
    • Joined on 02-19-2007, 8:31 PM
    • Posts 48

     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
    • Loading...
    • ks2007
    • Joined on 09-24-2007, 9:14 PM
    • Posts 221

    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
    • Loading...
    • louiechung
    • Joined on 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
    • Loading...
    • N1ck73
    • Joined on 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()

          }

    }

Page 2 of 2 (22 items) < Previous 1 2