I'm hoping to find a way to apply my alternating row pattern to a single datagridview column.
I have a windows forms application using vb.net. Right Now I have a pattern that changes the backcolor of every other datagridview cell to a different color. My pattern is white then light blue. I've included an image below and my code. This code applies this pattern to the entire datagridview, but I only want to apply it to one, for instance the second column index.
With Me.DataGridView1
.DefaultCellStyle.BackColor = Color.White
.AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
End With
You can add an if statement to your datagridview's cellformatting event like this:
Private Sub DataGridView1_ConditionalFormatting_StatusCell(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If e.ColumnIndex = 2 Then
With Me.DataGridView1
If e.RowIndex Mod 2 = 0 Then
e.CellStyle.ForeColor = Color.White
Else
e.CellStyle.ForeColor = Color.AliceBlue
End If
End With
End If
End Sub
If you want to give alternate style to some column from grid use cellformating event like this
Private Sub TDBGrid1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles TDBGrid1.CellFormatting
With TDBGrid1
If Not e.RowIndex Mod 2 = 0 AndAlso e.ColumnIndex > 3 Then
e.CellStyle.BackColor = Color.Cyan
Else
e.CellStyle.BackColor = Color.White
End If
End With
End Sub
Here i wanted alternate style to column index which is greater than 3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.