DataGridView 设置某行的颜色
方式一
AddHandler Me.TableViewLotFamily.DataGridView.RowPrePaint, AddressOf dgvPrePainting
Private Sub dgvPrePainting(sender As Object, e As DataGridViewRowPrePaintEventArgs)
If _RowIndex >= 0 AndAlso e.RowIndex = _RowIndex Then
Dim br As SolidBrush = New SolidBrush(Color.LightBlue)
e.Graphics.FillRectangle(br, e.RowBounds)
'e.Graphics.DrawRectangle(Pens.Gray, e.RowBounds)
ElseIf e.RowIndex >= 0 Then
Dim br As SolidBrush = New SolidBrush(Color.White)
e.Graphics.FillRectangle(br, e.RowBounds)
'e.Graphics.DrawRectangle(Pens.Gray, e.RowBounds) 设置边框
End If
End Sub
方式二
CType(sender, DataGridView).Rows(e.RowIndex).Selected=True 改变颜色
If TableViewLotFamily.DataGridView.Rows.Count > 0 Then
TableViewLotFamily.DataGridView.Rows(0).Selected = False
Dim lotList As List(Of LotLocationInfoDetail) = lotDetail.Where(Function(p) p.LotID = LotData.LotId).ToList
If lotList.Count > 0 Then
For idx2 As Integer = 0 To TableViewLotFamily.DataGridView.Rows.Count - 1
If TableViewLotFamily.DataGridView.Rows(idx2).Cells(0).Value.ToString = LotData.LotId Then
'_RowIndex = idx2
TableViewLotFamily.DataGridView.Rows(idx2).Selected = True
Exit For
End If
Next
'AddHandler Me.TableViewLotFamily.DataGridView.RowPrePaint, AddressOf dgvPrePainting
End If
End If
Private Sub DataGridView1_RowPrePaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
Try
e.PaintParts = e.PaintParts And Not DataGridViewPaintParts.Background
Dim r1, r2 As System.Drawing.Rectangle
Dim c1, c2 As Color
If e.RowIndex Mod 2 Then
c1 = Color.White ' Me.mEvenRowStartColor ' DataGridView1.RowsDefaultCellStyle.BackColor
c2 = Color.White ' Me.mEvenRowEndColor
Else
c1 = Color.White ' Me.mOddRowStartColor ' DataGridView1.AlternatingRowsDefaultCellStyle.BackColor
c2 = Color.White ' Me.mOddRowEndColor
End If
If CType(sender, DataGridView).Rows(e.RowIndex).Selected Then
c1 = Color.White ' Me.mHilightRowStartColor
c2 = Color.LightBlue ' Me.mHilightRowEndColor
End If
'e.Graphics.SmoothingMode = SmoothingMode.HighQuality
'Dim grdBrush1 As LinearGradientBrush
Dim normalBrush As SolidBrush
If CType(sender, DataGridView).Rows(e.RowIndex).Selected Then
r1 = New System.Drawing.Rectangle(e.RowBounds.Left, e.RowBounds.Top, e.RowBounds.Width, e.RowBounds.Height)
r2 = New System.Drawing.Rectangle(e.RowBounds.Left + 1, e.RowBounds.Top + 1, e.RowBounds.Width - 3, e.RowBounds.Height - 3)
'grdBrush1 = New LinearGradientBrush(r1, c1, c2, LinearGradientMode.ForwardDiagonal)
normalBrush = New SolidBrush(c2)
e.Graphics.FillRectangle(normalBrush, r1)
'Dim outline_pen As New Pen(Me.mHilightRectColor)
'e.Graphics.DrawRectangle(outline_pen, r1)
'outline_pen.Dispose()
Else
r1 = New System.Drawing.Rectangle(e.RowBounds.Left, e.RowBounds.Top, e.RowBounds.Width, e.RowBounds.Height)
'grdBrush1 = New LinearGradientBrush(r1, c1, c2, LinearGradientMode.ForwardDiagonal)
normalBrush = New SolidBrush(c1)
e.Graphics.FillRectangle(normalBrush, r1)
End If
'e.Graphics.DrawString(sender.Text, sender.Font, Brushes.DarkBlue, (sender.Width - e.Graphics.MeasureString(sender.Text, sender.Font).Width), 2)
'grdBrush1.Dispose()
Catch ex As Exception
End Try
End Sub
介绍了两种在C#中更改DataGridView行颜色的方法,包括通过Selected属性改变行颜色的示例代码。
1万+

被折叠的 条评论
为什么被折叠?



