|
|
Title | Color the current row in a DataGridView control in Visual Basic 2005 |
Description | This example shows how to color the current row in a DataGridView control in Visual Basic 2005. |
Keywords | DataGridView, color row, current row, row, column, Visual Basic 2005, VB 2005 |
Categories | Controls, VB.NET |
|
|
When the form loads, make a DataGridViewCellStyle object that sets the desired row foreground and background colors. If you want the row to look just like the selected cell, set BackColor to SystemColors.Highlight and set ForeColor to SystemColors.HighlightText (although then the user won't be able to tell which column is selected).
|
|
Private m_SelectedStyle As DataGridViewCellStyle
Private m_SelectedRow As Integer = -1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
m_SelectedStyle = New DataGridViewCellStyle()
m_SelectedStyle.BackColor = Color.LightBlue
'm_SelectedStyle.BackColor = SystemColors.Highlight
'm_SelectedStyle.ForeColor = SystemColors.HighlightText
...
End Sub
|
|
In the SelectionChanged event handler, reset the previously selected row's DefaultCellStyle property to Nothing. Then set thue newly selected row's DefaultCellStyle property to the style you created earlier.
|
|
' Set the default style for the selected row.
Private Sub DataGridView1_SelectionChanged(ByVal sender As _
Object, ByVal e As System.EventArgs) Handles _
DataGridView1.SelectionChanged
If m_SelectedRow >= 0 Then
DataGridView1.Rows(m_SelectedRow).DefaultCellStyle _
= Nothing
End If
m_SelectedRow = DataGridView1.CurrentRow.Index
DataGridView1.CurrentRow.DefaultCellStyle = _
m_SelectedStyle
End Sub
|
|
|
|
|
|