- This topic helps you learn how to change DataGridView rows color in VB programming.
- You can easily change the DataGrid row color with the help of DataGridViewCellStyle.
DataGridView – Change Datagridview rows color in VB
- The DataGridView control provides a customizable table for displaying data.
- DataGridView control is designed to be a complete solution for displaying tabular data with Windows Forms.
- DataGridView class allows us to customization of cells, rows, columns, and borders through the use of its properties
- Each cell within the DataGridView control can have its own style, such as text format, background color, foreground color, font etc.
- All cells derive from the DataGridViewCell base class.
DataGridViewCellStyle Class – Change Datagridview rows color in VB
- DataGridViewCellStyle Class represents the formatting and style information applied to individual cells within a DataGridView control.
Change Datagridview rows color in VB – Working
- First creates an object for the DataGridViewCellStyle which controls the DataGridView rows foreground and background color.
|
1 2 3 4 5 6 |
Dim grid_style As DataGridViewCellStyle Dim Sel_Row As Integer = -1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load grid_style = New DataGridViewCellStyle() grid_style.BackColor = Color.LightCoral End Sub |
- In the SelectionChanged event handler,It reset the previously selected row’s DefaultCellStyle property to Nothing.
- Then set newly selected row’s DefaultCellStyle property to the style that created at frame load.
|
1 2 3 4 5 6 7 |
Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged If Sel_Row >= 0 Then DataGridView1.Rows(Sel_Row).DefaultCellStyle = Nothing End If Sel_Row = DataGridView1.CurrentRow.Index DataGridView1.CurrentRow.DefaultCellStyle = grid_style End Sub |
Change Datagridview rows color in VB – Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Public Class Form1 Dim grid_style As DataGridViewCellStyle Dim Sel_Row As Integer = -1 Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged If Sel_Row >= 0 Then DataGridView1.Rows(Sel_Row).DefaultCellStyle = Nothing End If Sel_Row = DataGridView1.CurrentRow.Index DataGridView1.CurrentRow.DefaultCellStyle = grid_style End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load grid_style = New DataGridViewCellStyle() grid_style.BackColor = Color.LightCoral End Sub End Class |
Change Datagridview rows color in VB
If you have any suggestions or doubts, please contact us…






