HOWTO:将DataGrid中满足条件的行设为不同的背景色(winforms)

怎样将DataGrid中满足条件的行设为不同的背景色

谢谢!
---------------------------------------------------------------

哦,才又写了测试了下。原来的哪个的确有点乱。呵呵
思路是这样的。你要重写个datagridtextboxcolumn(继承自datagridtextboxcolumn),在其中公开一个事件,这个事件在onpaint触发,这样你就可以通过这个事件委托的参数来控制颜色的变化。
主要的部分有3个
第一:继承的控件:
代码如下:
Public Class mycolordatagridtextboxcolumn
Inherits System.Windows.Forms.DataGridTextBoxColumn

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'UserControl 重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。

  1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()   
  2components = New System.ComponentModel.Container()   
  3End Sub   
  4  
  5#End Region   
  6Private m_row As Integer '行号   
  7  
  8Private m_col As Integer '列号   
  9  
 10Private m_colname As String '列名   
 11  
 12Public Delegate Sub EnableCellEventHandler(ByVal sender As Object, ByVal e As DataGridEnableEventArgs)   
 13Public Event setcolorandenable As EnableCellEventHandler   
 14  
 15Public Sub New(ByVal column As Integer)   
 16MyBase.New()   
 17m_col = column   
 18End Sub   
 19  
 20Public Sub New(ByVal column As String)   
 21MyBase.New()   
 22m_colname = column   
 23End Sub   
 24  
 25Protected 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)   
 26Dim enabled As Boolean   
 27enabled = True   
 28Dim e As DataGridEnableEventArgs   
 29'创建一个实例   
 30e = New DataGridEnableEventArgs(rowNum, m_colname, enabled) '首先是可见的。   
 31RaiseEvent setcolorandenable(Me, e) '触发事件   
 32'这里取得默认的e参数的前背景色   
 33backBrush = New SolidBrush(e.bcolor)   
 34foreBrush = New SolidBrush(e.fcolor)   
 35'绘制颜色   
 36MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)   
 37''设置文字的显示控制   
 38'If e.setvalue &lt;&gt; "" Then   
 39' MyBase.PaintText(g, bounds, e.setvalue, alignToRight)   
 40'End If   
 41End Sub   
 42'光标可入否   
 43Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)   
 44  
 45Dim enabled As Boolean   
 46enabled = True   
 47Dim e As DataGridEnableEventArgs   
 48e = New DataGridEnableEventArgs(rowNum, m_colname, enabled)   
 49RaiseEvent setcolorandenable(Me, e)   
 50If e.EnableValue Then   
 51MyBase.Edit(source, rowNum, bounds, [readOnly], instantText, cellIsVisible)   
 52End If   
 53End Sub   
 54  
 55End Class   
 56  
 57\-----------------------------------   
 58第二。用来处理事件委托的参数类   
 59Public Class DataGridEnableEventArgs   
 60Inherits EventArgs   
 61  
 62Private m_column As Integer '列号   
 63  
 64Private m_row As Integer '行号   
 65  
 66Private m_columnname As String '列名称   
 67  
 68Private m_enablevalue As Boolean '单元格光标可进入否   
 69  
 70Private m_fcolor As Color = Color.Black '默认前景色   
 71  
 72Private m_bcolor As Color = Color.White '默认背景色   
 73  
 74Private m_setvalue As String = ""   
 75  
 76Public Sub New(ByVal row As Integer, ByVal col As Integer, ByVal val As Boolean)   
 77MyBase.New()   
 78m_row = row   
 79m_column = col   
 80m_enablevalue = val   
 81End Sub   
 82  
 83Public Sub New(ByVal row As Integer, ByVal col As String, ByVal val As Boolean)   
 84MyBase.New()   
 85m_row = row   
 86m_columnname = col   
 87m_enablevalue = val   
 88End Sub   
 89'得到列的属性   
 90Public Property Column() As Integer   
 91Get   
 92Return m_column   
 93End Get   
 94Set(ByVal Value As Integer)   
 95If Value &gt;= 0 Then   
 96m_column = Value   
 97End If   
 98End Set   
 99End Property   
100'得到列名的属性   
101Public Property Columnname() As String   
102Get   
103Return m_columnname   
104End Get   
105Set(ByVal Value As String)   
106If Value &gt;= 0 Then   
107m_columnname = Value   
108End If   
109End Set   
110End Property   
111'得到行的属性   
112Public Property Row() As Integer   
113Get   
114Return m_row   
115End Get   
116Set(ByVal Value As Integer)   
117If Value &gt;= 0 Then   
118m_row = Value   
119End If   
120End Set   
121End Property   
122'光标可入否   
123Public Property EnableValue() As Boolean   
124Get   
125Return m_enablevalue   
126End Get   
127Set(ByVal Value As Boolean)   
128m_enablevalue = Value   
129End Set   
130End Property   
131'前景色   
132Public Property fcolor() As Color   
133Get   
134Return m_fcolor   
135End Get   
136Set(ByVal Value As Color)   
137m_fcolor = Value   
138End Set   
139End Property   
140'背景色   
141Public Property bcolor() As Color   
142Get   
143Return m_bcolor   
144End Get   
145Set(ByVal Value As Color)   
146m_bcolor = Value   
147End Set   
148End Property   
149'修改值   
150Public Property setvalue() As String   
151Get   
152Return m_setvalue   
153End Get   
154Set(ByVal Value As String)   
155m_setvalue = Value   
156End Set   
157End Property   
158End Class   
159\-------------------------   
160  
161第三。用来测试程序的代码设置。   
162Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load   
163Dim conn As New SqlConnection("server=localhost;uid=sa;pwd=;database=jiang")   
164Dim cmd As New SqlCommand("select * from city ", conn)   
165Dim tbl As New DataTable()   
166Dim sqldpr As New SqlDataAdapter(cmd)   
167Try   
168sqldpr.Fill(tbl)   
169tbl.DefaultView.AllowNew = False   
170Me.DataGrid1.ReadOnly = True   
171Me.DataGrid1.DataSource = tbl   
172Me.DataGrid1.TableStyles.Add(getmystyle(tbl))   
173Catch ex As Exception   
174MessageBox.Show(ex.Message)   
175End Try   
176End Sub   
177Private Function getmystyle(ByVal tbl As DataTable) As DataGridTableStyle   
178Dim count As Integer = tbl.Columns.Count   
179Dim i As Integer   
180Dim style As New DataGridTableStyle()   
181style.MappingName = tbl.TableName   
182style.RowHeaderWidth = 15   
183For i = 0 To count - 1   
184Dim c As New mycolordatagridtextboxcolumn()   
185c.HeaderText = tbl.Columns(i).ColumnName   
186c.MappingName = tbl.Columns(i).ColumnName   
187AddHandler c.setcolorandenable, AddressOf mysetcolor   
188style.GridColumnStyles.Add(c)   
189Next   
190Return style   
191End Function   
192Private Sub mysetcolor(ByVal sender As Object, ByVal e As DataGridEnableEventArgs)   
193Dim tbl As DataTable = CType(Me.DataGrid1.DataSource, DataTable)   
194Dim m As Integer   
195m = tbl.Rows(e.Row).Item("pid")   
196If m &gt; 1 Then   
197e.bcolor = Color.Red   
198e.EnableValue = False   
199Else   
200e.bcolor = Color.Green   
201e.EnableValue = True   
202End If   
203End Sub   
204  
205至于光标可入否和其他设置,可以通过修改e参数的属性达到,这里不再多说了。</system.diagnostics.debuggerstepthrough()>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus