Dim arrList AsNew ArrayList For i AsInteger=0To mytable.Rows.Count -1 IfCInt(mytable.Rows(i)("Bladder_Max")) >CInt(mytable.Rows(i)("Bladder_Count")) Then arrList.Add(0) Else arrList.Add(1) EndIf Next Dim TextCol As DataGridColoredTextBoxColumn TextCol =New DataGridColoredTextBoxColumn(arrList, mytable.Rows.Count, Color.White, Color.Red) TextCol.MappingName ="Bladder_Name" TextCol.HeaderText ="Bladder名称" TextCol.Alignment = System.Windows.Forms.HorizontalAlignment.Center TextCol.Width =Me.DataGrid1.Width /6-10 TextCol.TextBox.Enabled =False ts1.GridColumnStyles.Add(TextCol) '........ Me.DataGrid1.TableStyles.Clear() Me.DataGrid1.TableStyles.Add(ts1)
PublicClass DataGridColoredTextBoxColumntBoxColumnClass DataGridColoredTextBoxColumntBoxColumn Private m_Row AsInteger=-1 Private m_BackColor As Color = Color.White Private m_ForeColor As Color = Color.Black Private defalutBack As Brush =New SolidBrush(Color.White) Private defalutFore As Brush =New SolidBrush(Color.Black) Private m_Smt AsInteger Private m_Array As ArrayList Sub New()SubNew(ByVal Row AsInteger, ByVal [ForeColor] As Color) m_Row = Row m_ForeColor = ForeColor End Sub Sub New()SubNew(ByVal row AsInteger, ByVal [ForeColor] As Color, ByVal [BackColor] As Color) m_Row = row m_ForeColor = ForeColor m_BackColor = BackColor End Sub Sub New()SubNew(ByVal row AsInteger, ByVal cnt AsInteger, ByVal [ForeColor] As Color, ByVal [BackColor] As Color) m_Row = row m_Smt = cnt m_ForeColor = ForeColor m_BackColor = BackColor End Sub Sub New()SubNew(ByVal arrList As ArrayList, ByVal cnt AsInteger, ByVal [ForeColor] As Color, ByVal [BackColor] As Color) m_Row =-1 m_Array = arrList m_Smt = cnt m_ForeColor = ForeColor m_BackColor = BackColor End Sub ProtectedOverloadsOverridesSub Paint()Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum AsInteger, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight AsBoolean) Try If rowNum < m_Smt Then If m_Array(rowNum) =0Then Else backBrush =New SolidBrush(m_BackColor) foreBrush =New SolidBrush(m_ForeColor) EndIf EndIf Catch ex As Exception ' empty catch Finally MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight) EndTry End Sub End Class
前几日,搜了好久有关vb.net中DataGrid指定行颜色设置的相关信息,拜读了很多力作。稍微修改了以下,用了以下还不错。 Dim arrList As New ArrayList For i As Integer = 0 To mytable.Rows.Count - 1 If CInt(mytab