VB.NET中关于DataGrid颜色的自定义。

类别:.NET开发 点击:0 评论:0 推荐:

 

     近来项目用到了一个类似WEB控件DataGrid中自定义行或列的颜色的功能,然而应用却是在WIN的窗体下,实现起来无法使用类似JavaScript的脚本注册的功能来动态完成,十分着急,察看了CSDN的一些关于WinForm下的关于DataGrid的资料,看到这样的一篇介绍DG结构的美文,题目是《Henry手记:WinForm Datagrid结构剖析》,作者是韩睿(Latitude,其中介绍了WIN  DG的颜色的定义,但是主要是针对每一个Cell的。

我们需要的则是标记某一行的数据,用颜色突出显示,所以作了部分改动,现在把部分代码张贴出来供大家参考:

1.  基础类出自韩睿

URL: http://www.csdn.net/develop/read_article.asp?id=15686

  Public Class DataGridColoredTextBoxColumn

 

            Inherits DataGridTextBoxColumn

 

            Public rowcollection As New Collection()

 

            Public BackColor() As Color

 

            Public ForeColor() As Color

 

 

            Private Function GetText(ByVal Value As Object) As String

                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))

 

                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

 

                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

 

 

2.关于行颜色定义的类:

Imports System.Windows.Forms

Namespace Truck_WEB

 

    Public Class DrawDGClass

 

        Public Class ReDrawDataDridControls : Inherits DataGridColoredTextBoxColumn

 

            Public Sub DrawCorol(ByRef DG As DataGrid, Optional ByVal CurrentRowindex As Integer = 0)

               '设置选中的行的颜色,默认是第一行选中。

                 Dim dt As DataTable

                Dim ts As New DataGridTableStyle()

                ts.AllowSorting = False

                Dim aColumnTextColumn As DataGridColoredTextBoxColumn

                dt = CType(DG.DataSource, DataTable)

                ts.MappingName = CType(DG.DataSource, DataTable).TableName

                DG.TableStyles.Clear()

                Dim numCols As Integer

                numCols = dt.Columns.Count

                Dim i, j As Integer

                i = 0

                j = 0

 

                Do While (i < numCols)

 

                    aColumnTextColumn = New DataGridColoredTextBoxColumn()

 

                    Dim rowindex As Integer = 0

 

 

                    For rowindex = 0 To dt.Rows.Count - 1

                        Dim StrSel As String

                        Dim MyForeCorol, MyBackCorol As Color

                        aColumnTextColumn.rowcollection.Add(rowindex)

                        If rowindex = CurrentRowindex Then

                                MyForeCorol = Color.White

                                MyBackCorol = Color.DarkSlateBlue

                        else

 

                                MyForeCorol = Color.DarkSlateBlue

                                MyBackCorol = Color.White

                        End If

                       

                        ReDim Preserve aColumnTextColumn.ForeColor(aColumnTextColumn.rowcollection.Count)

                        ReDim Preserve aColumnTextColumn.BackColor(aColumnTextColumn.rowcollection.Count)

                        aColumnTextColumn.ForeColor(rowindex) = MyForeCorol

                        aColumnTextColumn.BackColor(rowindex) = MyBackCorol

                    Next

 

 

                    '要更改列头名,请改下句的HeaderText值

                    aColumnTextColumn.HeaderText = dt.Columns(i).ColumnName

 

                    aColumnTextColumn.MappingName = dt.Columns(i).ColumnName

 

                    ts.GridColumnStyles.Add(aColumnTextColumn)

                    i = (i + 1)

                Loop

                DG.TableStyles.Add(ts)

 

            End Sub

        End Class

    End Class

End Namespace

 

 

以上是设定选中单行的颜色为反色,各位还可以借题发挥一下!例如设置颜色,等等。

在此向《Henry手记:WinForm Datagrid结构剖析》的作者韩睿致谢!

 

以后我会尽量完善这个DrawDG的类,为大家提供方便!

本文地址:http://com.8s8s.com/it/it45633.htm