Datagrid 的格式化[1]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

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

本文关键:Datagrid 的格式化
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top