Problem with selecting rows in gridview and passing valuee to a query statement

Hello,
How do you select value from a gridview? I would like to pass values of rows selected to a query with the code below, but the grid seems to be frozen and I can't find a property for the grid that allows me to select rows.

Thanks,
Victor
      Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.SelectedIndexChanged
        Dim DB As New AOP29DBDataContext
        If GridView1.SelectedValue = "Receiver" Then
            Dim Query = From p In DB.Receivers
                        Select p
            GridView2.DataSource = Query
            GridView2.DataBind()
        End If
        If GridView1.SelectedValue = "Donor" Then
            Dim Query = From p In DB.Donors
                        Select p
            GridView2.DataSource = Query
            GridView2.DataBind()
        End If
    End Sub
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vcharlesAuthor Commented:
Hello,
When I test the code in my previous post using a command button, I recive the fiollowing error:
"Data keys must be specified on GridView 'GridView1' before the selected data keys can be retrieved.  Use the DataKeyNames property to specify data keys".
Please help with the proper syntax.
Thanks,
Victor
BurniePCommented:
To define DataKeyNames in your gridview you need to add the DataKeyNames attribute in your GridView declaration.

Something like :

<asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="False"  DataKeyNames="ID,UserID">
</asp:GridView>

You can set more than one.  And the value you put there is the name of the column(s) you want to retrieve server side.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thanks, how do you hide the id column while you are still selecting it with your query?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.