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

VB.NET中关于DataGrid颜色的自定义。 (转)[@more@]

 

  近来项目用到了一个类似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 DataGridColoredTextBoxColumnXML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

 

  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的类,为大家提供方便!


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752019/viewspace-957549/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752019/viewspace-957549/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值