WinForm的DataGrid在显示的时候,某个Cell得颜色要变成红的(VB.NET)

其实这个问题网上老早就有了,在此只是整理了一下,可以使多个Cell变成不同的颜色。

 

Public Class DataGridColoredTextBoxColumn

    Inherits DataGridTextBoxColumn

    Private m_Row As Integer = -1

    Private m_BackColor As Color = Color.White

    Private m_ForeColor As Color = Color.Black

 

    Sub New(ByVal Row As Integer, ByVal [ForeColor] As Color)

        m_Row = Row

        m_ForeColor = ForeColor

    End Sub

 

    Sub New(ByVal row As Integer, ByVal [ForeColor] As Color, ByVal [BackColor] As Color)

        m_Row = row

        m_ForeColor = ForeColor

        m_BackColor = BackColor

    End Sub

 

    Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _

                            ByVal bounds As Rectangle, ByVal source As CurrencyManager, _

                            ByVal rowNum As Integer, ByVal backBrush As Brush, _

                            ByVal foreBrush As Brush, ByVal alignToRight As Boolean)

        Try

            If m_Row <> -1 Then

                If rowNum = m_Row Then

                    backBrush = New SolidBrush(m_BackColor)

                    foreBrush = New SolidBrush(m_ForeColor)

                End If

            End If

        Catch ex As Exception

            ' empty catch

        Finally

            MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)

        End Try

    End Sub

 

End Class

 

调用代码:

Imports System.Data.OleDb

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim ada As New OleDbDataAdapter("select * from class_info", _

                 "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= f:/StudentDB.mdb")

        Dim mytable As New DataTable

        ada.Fill(mytable)

 

        Dim mGridStyle As New DataGridTableStyle

        mGridStyle.MappingName = mytable.TableName

        Dim mColumnStyle As DataGridColumnStyle

 

        Dim column As Integer = 0

 

        For Each c As DataColumn In mytable.Columns

            If column = 1 Then     'Cell(0,1)变为红色

                mColumnStyle = New DataGridColoredTextBoxColumn(0, Color.Red)

            ElseIf column = 2 Then 'Cell(1,2)变为蓝色

                mColumnStyle = New DataGridColoredTextBoxColumn(1, Color.Blue)

            Else                   '其它不变

                mColumnStyle = New DataGridTextBoxColumn

            End If

 

            With mColumnStyle

                .MappingName = c.ColumnName

                .HeaderText = c.ColumnName

            End With

            mGridStyle.GridColumnStyles.Add(mColumnStyle)

            column += 1

        Next

 

        DataGrid1.TableStyles.Add(mGridStyle)

        DataGrid1.DataSource = mytable

 

    End Sub

 

End Class

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值