Highlight and Edit on DataGridView Row

retkehing

Well-known member
Joined
Feb 5, 2006
Messages
153
Programming Experience
Beginner
How to highlight a particular row when user single click on any of the cell of that row? How to edit a particular cell when user single click on that cell but not double click? Thank you.
 
How to highlight a particular row when user single click on any of the cell of that row?
Set SelectionMode property to FullRowSelect.
How to edit a particular cell when user single click on that cell but not double click?
VB.NET:
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView1.CellClick
    Dim dgv As DataGridView = sender
    dgv.BeginEdit(True)
End Sub
 
I used the following code but sometimes if i click on different cell randomly, it won't really highlight the particular row. May i know why? Thank you.

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] DataGridView1_CellContentClick([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.DataGridViewCellEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] DataGridView1.CellContentClick[/SIZE]
[SIZE=2]DataGridView1.Rows(e.RowIndex).Selected = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
When i assign an action under the DataGridView1_CellContentClick and click on the title row, an error will occur. May i know how to lock the title row so then it won't trigger the action under DataGridView1_CellContentClick? Thank you.
 
What is e.RowIndex value when this happens ? Is is '-1' ? If so, you can check if e.RowIndex is this value and escape that with the IF-THEN statement. So there's no need for you to write a new DataGridView control just yet.
 
Back
Top