Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to access the selected value in a GridView in VB

Status
Not open for further replies.

Andel

Programmer
Joined
Feb 15, 2001
Messages
366
Location
US
How could I access a cell value in a GridView control using VB? Basically, i have a gridview that displays records from a table. When I select a record from the Gridview, I want a certain column (from the selected record) to be displayed in a label control.

Thanks in advance.

Andel
 
Code:
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.SelectedIndexChanged

'x is the zero based index(integer) of the cell's text you 'want.
YourTextBox.Text = e.Row.Cells(x).Text

Jim
 

Thanks for responding Jim.

When I doubleclick the my gridview the following will get populated on the vb file:

Protected Sub GridView_Riddles_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView_Riddles.SelectedIndexChanged

End Sub

I could not get the "ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs", instead i get the System.EventArgs. I tried to change it manually but I get the following:



Method 'Protected Sub GridView_Riddles_SelectedIndexChanged(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs)' cannot handle Event 'Public Event SelectedIndexChanged(sender As Object, e As System.EventArgs)' because they do not have the same signature.

Any other idea?


 
I also tried the following butno luck.

Protected Sub GridView_Riddles_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView_Riddles.SelectedIndexChanged
lblRiddle.Text = GridView_Riddles.SelectedRow.Cells(1).Text

End Sub


The above code won't generate an error but I don't see anything in my lblRiddle.text field.




 
Do you have the select link button showing in the grid for the user to click on to select a row?
 
Thanks Jim

Ok, the code above works now. I think what happend was that I made the fields in my gridview as template. When i recreated the gridview. it started working.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top