Coloring a clicked line in a datagridview

Posted in: Forms and Grids
By dePoPo
Mar 6, 2009 - 11:49:38 AM

Coloring the clicked line in a datagridview, while at the same time setting another color on all other lines (in this case white to remove highlighting, but any color is usable)


Dim n_current_row As Integer
Dim n_current_column As Integer
Dim n_clicked_id As Integer
Dim d_row As DataGridViewRow
Dim f As Integer
 
 
Dim n_row As Integer
 
' detect the clicked line
 n_current_row = e.RowIndex
 n_current_column = e.ColumnIndex
 If n_current_row < 0 Then Exit Sub ' no data
 d_row = DataGridView1.Rows(n_current_row) ' d_row is now a row object with all fields
 n_clicked_id = d_row.Cells(0).Value ' asuming field(0) contains the id
 
' color the line that was clicked, and remove color from others
 For n_row = 0 To DataGridView1.RowCount - 1
  d_row = DataGridView1.Rows(n_row)
  If n_row = n_current_row Then
  For f = 0 To DataGridView1.ColumnCount - 1
  d_row.Cells(f).Style.BackColor = Color.FromArgb(187, 255, 187)
  Next
  Else
  For f=0 to DataGridView1.ColumnCount-1
  d_row.Cells(f).Style.BackColor = Color.White
  Next
  End If
 Next


Visitor Comments