Multiple DataKeyNames From One GridView


Well-known member
Nov 7, 2005
Programming Experience
Hello all

I have a webform with three GridViews on it. The first GridView populates a summary of information at the Form_Load event. I have set this gridview to be selectable. When an item is selected off this gridview I need it to pass two different values - one to each of the subsequent gridviews (GridView2, GridView3). I have set two DataKeyNames in the properties of GridView1. From checking around I guess that I need to set up the Selecting event for GridView2 and GridView3 for this to work - which is where I am stuck.

This is what I have so far
    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
 Handles GridView1.SelectedIndexChanged
        m = Me.GridView1.DataKeys(0).ToString
        A = Me.GridView1.DataKeys(1).ToString
    End Sub

    Protected Sub SqlDataSource2_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) 
Handles SqlDataSource2.Selecting
        e.Command.Parameters("@messid").Value = m
    End Sub

    Protected Sub SqlDataSource3_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) 
Handles SqlDataSource3.Selecting
        e.Command.Parameters("@asset").Value = A
    End Sub

When I debug this code
m = Me.GridView1.DataKeys(0).ToString
it literally returns System.Web.UI.WebControls.Datakey as a string value. I get the same results for the next line. My code then bombs out when I try to databind - Cannot convert nvarchar to numeric. The two datakeys are numeric values. How do I get the "actual" values of the datakeys for the selected row?

Any help is appreciated. Thanks
Top Bottom