Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi
when a user selects a single cell in my gridview i want to select the entire row of data. Or find the value of another cell in the same row.

for eg. I'll have 5 fields displaying in a gridview from a database.
(note : this is a datagridview that is displayed on a form in VB.net)
__________________________________________________________
name | Surname |ID number | contact Number| Gender |
----------------------------------------------------------
john| Garth |012251 | 074 551 2102 | Male |

when I select John or any other field I want to store the ID number in a variable
(even when I select the ID field)

Thank you very much.
Posted

1 solution

U add a action to the datagrid and the int is the selectedrow
With that int u can select all the cells within that row.

VB
Private Sub dtg_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtg.CellContentClick
        Dim int As Integer = Me.dtg.CurrentCell.RowIndex
        Dim FirstColumnSelectedRecord As String
        'U can use a column index or the columnheader
        FirstColumnSelectedRecord =  Me.dtg.Item(0, int).Value.ToString
 
Share this answer
 
Comments
Member 9374423 23-Aug-12 8:32am    
what is the dtg your using?
Member 9374423 23-Aug-12 8:33am    
nevermind I got it thank you very much!
Member 9374423 24-Aug-12 3:28am    
it works fine except when i select the ID field or the first field("Name" in this case) ,any updates?

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