#Region "改变单元格色彩 格式化串 文本内容的列"
Public Class DataGridMyTextBoxColumn
Inherits DataGridTextBoxColumn
Event PaintCell(ByRef CellColor As Color, ByRef CellForeColor As Color, ByRef Font As System.Drawing.Font, ByVal CellValue As Object, ByVal Source As CurrencyManager, ByVal RowNum As Integer) Private m_MyFormate As String
Public Sub New()
End Sub Public Property MyFormat() As String
Get
Return m_MyFormate
End Get
Set(ByVal Value As String)
m_MyFormate = Value.ToUpper
Me.ReadOnly = True
End Set
End Property Protected Overrides Sub Abort(ByVal RowNum As Integer)
MyBase.Abort(RowNum)
End Sub Protected Overrides Function Commit(ByVal DataSource As CurrencyManager, ByVal RowNum As Integer) As Boolean
If Me.ReadOnly Then Return False
Return MyBase.Commit(DataSource, RowNum)
End Function
Protected Overloads Overrides Sub Edit(ByVal Source As CurrencyManager, ByVal Rownum As Integer, ByVal Bounds As Rectangle, ByVal isReadOnly As Boolean, ByVal InstantText As String, ByVal CellIsVisible As Boolean)
'If me.ReadOnly Then Return '不能得到焦点
MyBase.Edit(Source, Rownum, Bounds, isReadOnly, InstantText, CellIsVisible)
End Sub ' return here your minimum height
Protected Overrides Function GetMinimumHeight() As Integer
Return 16
End Function '// return here your preferred height
Protected Overrides Function GetPreferredHeight(ByVal g As Graphics, ByVal Value As Object) As Integer
Return 16
End Function '// return here your preferred size
Protected Overrides Function GetPreferredSize(ByVal g As Graphics, ByVal Value As Object) As Size
Return New Size(75, 16)
End Function Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal Bounds As Rectangle, ByVal Source As CurrencyManager, ByVal RowNum As Integer)
Paint(g, Bounds, Source, RowNum, Nothing, Nothing, Nothing)
End Sub Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal Bounds As Rectangle, ByVal Source As CurrencyManager, ByVal RowNum As Integer, ByVal AlignToRight As Boolean)
Paint(g, Bounds, Source, RowNum, Nothing, Nothing, Nothing)
End Sub Protected 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)
'Select Case CStr(ColorID.GetColumnValueAtRow(Source, RowNum))
' Case Color.Blue.Name
' BackBrush = Brushes.Blue
' Case Color.Red.Name 'End Select Dim CellBackColor As Color = Me.DataGridTableStyle.DataGrid.BackColor
Dim CellForeColor As Color = Me.DataGridTableStyle.DataGrid.ForeColor, CellVal As Object, sVal As String, n As Integer
Dim font As System.Drawing.Font = Me.DataGridTableStyle.DataGrid.Font CellVal = GetColumnValueAtRow(Source, RowNum)
RaiseEvent PaintCell(CellBackColor, CellForeColor, font, CellVal, Source, RowNum) If IsNothing(CellVal) OrElse IsDBNull(CellVal) Then
sVal = ""
Else
sVal = CStr(CellVal)
End If
Try
If m_MyFormate.StartsWith("RIGHT") Then
n = CInt(m_MyFormate.Substring(5))
sVal = sVal.Substring(sVal.Length - n)
ElseIf m_MyFormate.StartsWith("LEFT") Then
n = CInt(m_MyFormate.Substring(4))
sVal = sVal.Substring(0, n)
End If
Catch
End Try
BackBrush = New SolidBrush(CellBackColor)
ForeBrush = New SolidBrush(CellForeColor) g.FillRectangle(BackBrush, Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height)
g.DrawString(sVal, font, ForeBrush, Bounds.X, Bounds.Y)
End SubEnd Class
Public Class DataGridMyTextBoxColumn
Inherits DataGridTextBoxColumn
Event PaintCell(ByRef CellColor As Color, ByRef CellForeColor As Color, ByRef Font As System.Drawing.Font, ByVal CellValue As Object, ByVal Source As CurrencyManager, ByVal RowNum As Integer) Private m_MyFormate As String
Public Sub New()
End Sub Public Property MyFormat() As String
Get
Return m_MyFormate
End Get
Set(ByVal Value As String)
m_MyFormate = Value.ToUpper
Me.ReadOnly = True
End Set
End Property Protected Overrides Sub Abort(ByVal RowNum As Integer)
MyBase.Abort(RowNum)
End Sub Protected Overrides Function Commit(ByVal DataSource As CurrencyManager, ByVal RowNum As Integer) As Boolean
If Me.ReadOnly Then Return False
Return MyBase.Commit(DataSource, RowNum)
End Function
Protected Overloads Overrides Sub Edit(ByVal Source As CurrencyManager, ByVal Rownum As Integer, ByVal Bounds As Rectangle, ByVal isReadOnly As Boolean, ByVal InstantText As String, ByVal CellIsVisible As Boolean)
'If me.ReadOnly Then Return '不能得到焦点
MyBase.Edit(Source, Rownum, Bounds, isReadOnly, InstantText, CellIsVisible)
End Sub ' return here your minimum height
Protected Overrides Function GetMinimumHeight() As Integer
Return 16
End Function '// return here your preferred height
Protected Overrides Function GetPreferredHeight(ByVal g As Graphics, ByVal Value As Object) As Integer
Return 16
End Function '// return here your preferred size
Protected Overrides Function GetPreferredSize(ByVal g As Graphics, ByVal Value As Object) As Size
Return New Size(75, 16)
End Function Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal Bounds As Rectangle, ByVal Source As CurrencyManager, ByVal RowNum As Integer)
Paint(g, Bounds, Source, RowNum, Nothing, Nothing, Nothing)
End Sub Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal Bounds As Rectangle, ByVal Source As CurrencyManager, ByVal RowNum As Integer, ByVal AlignToRight As Boolean)
Paint(g, Bounds, Source, RowNum, Nothing, Nothing, Nothing)
End Sub Protected 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)
'Select Case CStr(ColorID.GetColumnValueAtRow(Source, RowNum))
' Case Color.Blue.Name
' BackBrush = Brushes.Blue
' Case Color.Red.Name 'End Select Dim CellBackColor As Color = Me.DataGridTableStyle.DataGrid.BackColor
Dim CellForeColor As Color = Me.DataGridTableStyle.DataGrid.ForeColor, CellVal As Object, sVal As String, n As Integer
Dim font As System.Drawing.Font = Me.DataGridTableStyle.DataGrid.Font CellVal = GetColumnValueAtRow(Source, RowNum)
RaiseEvent PaintCell(CellBackColor, CellForeColor, font, CellVal, Source, RowNum) If IsNothing(CellVal) OrElse IsDBNull(CellVal) Then
sVal = ""
Else
sVal = CStr(CellVal)
End If
Try
If m_MyFormate.StartsWith("RIGHT") Then
n = CInt(m_MyFormate.Substring(5))
sVal = sVal.Substring(sVal.Length - n)
ElseIf m_MyFormate.StartsWith("LEFT") Then
n = CInt(m_MyFormate.Substring(4))
sVal = sVal.Substring(0, n)
End If
Catch
End Try
BackBrush = New SolidBrush(CellBackColor)
ForeBrush = New SolidBrush(CellForeColor) g.FillRectangle(BackBrush, Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height)
g.DrawString(sVal, font, ForeBrush, Bounds.X, Bounds.Y)
End SubEnd Class
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货