Datagrid 的格式化

Public Class DataGridColoredTextBoxColumn
    Inherits DataGridTextBoxColumn  '继承接口
    Public rowcollection As New Collection   '用于存放行号的集合
    Public BackColor() As Color '用于存放背景色的集合,索引与行号集号一致(集合索引减1)
    Public ForeColor() As Color '用于存放前景色的集合,索引与行号集号一致
    Private Function GetText(ByVal Value As Object) As String
        '为进行重绘读出textbox中的内容,这个函数是增加容错能力的,与实现机制无关
        If TypeOf (Value) Is System.DBNull Then
            Return NullText
        ElseIf Value Is Nothing Then
            Return ""
        Else
            Return Value.ToString
        End If
    End Function
    Protected Overloads Overrides Sub Paint( _
        ByVal g As System.Drawing.Graphics, _
        ByVal bounds As System.Drawing.Rectangle, _
        ByVal source As System.Windows.Forms.CurrencyManager, _
        ByVal rowNum As Integer, _
        ByVal backBrush As System.Drawing.Brush, _
        ByVal foreBrush As System.Drawing.Brush, _
        ByVal alignToRight As Boolean)
        Dim text As String
        '按默认模式重绘
        text = GetText(GetColumnValueAtRow(source, rowNum)) '这句就是上文提及的取数据
        If text = "(null)" Then
            text = ""
        End If
        backBrush = New SolidBrush(TextBox.BackColor) '背景色
        foreBrush = New SolidBrush(TextBox.ForeColor)  '前景色
        '防止用户没有定义集合大小
        ReDim Preserve BackColor(rowcollection.Count)
        ReDim Preserve ForeColor(rowcollection.Count)
        Dim i As Integer = 1 '集合索引从1开始
        Do While (i <= rowcollection.Count)
            If rowNum = Val(rowcollection.Item(i)) Then  '判断要集合中的行数是
                If Not BackColor(i - 1).IsEmpty Then '没有定义的就按默认色绘制
                    backBrush = New SolidBrush(BackColor(i - 1))
                End If
                If Not ForeColor(i - 1).IsEmpty Then '没有定义的就按默认色绘制
                    foreBrush = New SolidBrush(ForeColor(i - 1))
                End If
            End If
            i += 1
        Loop
        MyBase.PaintText(g, bounds, text, backBrush, foreBrush, alignToRight)
    End Sub
End Class

    Do While (i < numCols) '重绘所有的列
            aColumnTextColumn = New DataGridColoredTextBoxColumn
            j = 0
            If S_count > 0 Then
                For j = 0 To StopDe.Length - 1
                    aColumnTextColumn.rowcollection.Add(StopDe(j))
                    ReDim Preserve aColumnTextColumn.ForeColor(aColumnTextColumn.rowcollection.Count)
                    ReDim Preserve aColumnTextColumn.BackColor(aColumnTextColumn.rowcollection.Count)
                    aColumnTextColumn.ForeColor(j) = Color.Gray
                    aColumnTextColumn.BackColor(j) = Color.White
                Next
            End If
            If E_count > 0 Then
                For m = 0 To EndDe.Length - 1
                    aColumnTextColumn.rowcollection.Add(EndDe(m))
                    ReDim Preserve aColumnTextColumn.ForeColor(aColumnTextColumn.rowcollection.Count)
                    ReDim Preserve aColumnTextColumn.BackColor(aColumnTextColumn.rowcollection.Count)
                    aColumnTextColumn.ForeColor(j) = Color.Red
                    aColumnTextColumn.BackColor(j) = Color.White
                    j = j + 1
                Next
            End If
            aColumnTextColumn.HeaderText = dvd.Table.Columns(i).ColumnName
            aColumnTextColumn.MappingName = dvd.Table.Columns(i).ColumnName
            aColumnTextColumn.Alignment = HorizontalAlignment.Center
            ts.GridColumnStyles.Add(aColumnTextColumn)
            i = (i + 1)
        Loop
        Me.DgdDelegaInfo.TableStyles.Clear()
        Me.DgdDelegaInfo.TableStyles.Add(ts) 'datagrid

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值