Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello please i need help on how to get a gridview datakey value of a selected row that is binded manually from a linq to entity query. This is my query i used to bind the gridview
SQL
Dim searchClass = From cla In DemoSchool.RealClasses.OrderBy(Function(D) D.ClassCode).ThenBy(Function(P) P.ClassArm) _
                         Where cla.Session = drpSessionGrid.SelectedItem.Text
                            Select New With {Key .ClassSN = cla.ClassSN,
                                            Key .Class = cla.ClassCode,
                                            Key .ClassArm = cla.ClassArm}


       grdShowClassBySession.DataSource = searchClass
       grdShowClassBySession.DataBind()


The query works fine and the data is displayed on the gridview as expected, The problem is that i want to be able to retrieve the datakey of a selected row in the gridview selectedindexchanging method but i can't since the gridview is not bounded by a datasourceID like an entitydataSource control. I found a custom control from this url on codeproject
Simple GridView Radio Button Row Selector[^] .The control is able to fake the gridview selected row link button by using a radiobutton.But when i use it to get the datakey value of a selectedRow it gives an error saying Object not set to the instance of an object use the New Keyword
VB
Private Sub grdShowClassBySession_SelectedIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdShowClassBySession.SelectedIndexChanging
       Dim classS As String = grdShowClassBySession.DataKeys(grdShowClassBySession.SelectedIndex).Value.ToString




   End Sub

I have been cracking my head since and i have also passed by bedtime (hope i don't sleep in the office tomorrow) because of this problem.Any help is highly appreciated. If i can be pointed to a more workable solution to this problem i will be very grateful.Thanks as always
Posted
Updated 9-Sep-13 14:43pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900