DataGridView 设置某行的颜色

介绍了两种在C#中更改DataGridView行颜色的方法,包括通过Selected属性改变行颜色的示例代码。

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值