|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWhy reinvent the wheel when you can just get new hub caps? In this article, I will show how to gain access to the Using the codeThe first item is a no brainer, the
When I first realized these successions, I nearly gave up on the idea, but then I thought just maybe I could use inheritance to gain access to the I created three classes,
The Public Class DropDownListColumn
Inherits DataGridViewComboBoxColumn
Public Sub New()
'Set the type used in the DataGridView
Me.CellTemplate = New DropDownListCell
End Sub
End Class
The Public Class DropDownListCell
Inherits DataGridViewComboBoxCell
Public Overrides ReadOnly Property EditType() As Type
Get
' Return the type of the editing contol that
' the DropDownListCell uses.
Return GetType(DropDownListEditingControl)
End Get
End Property
Protected Overrides Function GetFormattedValue( _
ByVal value As Object, ByVal rowIndex As Integer, _
ByRef cellStyle As System.Windows.Forms.DataGridViewCellStyle, _
ByVal valueTypeConverter As System.ComponentModel.TypeConverter, _
ByVal formattedValueTypeConverter As _
System.ComponentModel.TypeConverter, _
ByVal context As _
System.Windows.Forms.DataGridViewDataErrorContexts) As Object
'Get object that is diplayed in the DataGridView cells
'with default formatting
Dim obj As Object = MyBase.GetFormattedValue( _
value, rowIndex, cellStyle, _
valueTypeConverter, formattedValueTypeConverter, _
context)
'If the text is prefixed with EXPIRED then make the font red
If Not IsNothing(obj) AndAlso obj.ToString.StartsWith("EXPIRED") Then
cellStyle.ForeColor = System.Drawing.Color.Red
End If
Return obj
End Function
End Class
The To change the Public Class DropDownListEditingControl
Inherits DataGridViewComboBoxEditingControl
Public Sub New()
MyBase.New()
'This will cause the DrawItem event to raise,
'allowing changes to be made to the appearance
'at run time
Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
'Make this a DropDownList
Me.DropDownStyle = ComboBoxStyle.DropDownList
End Sub
Protected Overrides Sub OnDrawItem( _
ByVal e As System.Windows.Forms.DrawItemEventArgs)
' Create a rectagle the size of the item container
Dim rec As New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, _
e.Bounds.Height)
If Me.Enabled Then
'If the item is being pointed to highlight it
If (e.State And DrawItemState.Focus) = DrawItemState.Focus Then
e.Graphics.FillRectangle( _
New SolidBrush(System.Drawing.SystemColors.Highlight), rec)
'The next two clauses will cause the item backcolor to alternate
ElseIf (e.State And DrawItemState.ComboBoxEdit) = _
DrawItemState.ComboBoxEdit _
OrElse e.Index Mod 2 = 1 Then
e.Graphics.FillRectangle( _
New SolidBrush(System.Drawing.SystemColors.Window), rec)
Else
e.Graphics.FillRectangle( _
New SolidBrush(System.Drawing.SystemColors.ControlLight), rec)
End If
Else
'Grey out the box it is disablbed
e.Graphics.FillRectangle( _
New SolidBrush(System.Drawing.SystemColors.Control), rec)
End If
'If there is an item
If e.Index > -1 Then
Dim obj As Object = Me.Items(e.Index)
'If the item is selected highlight it
If (e.State And DrawItemState.Focus) = DrawItemState.Focus Then
Dim HighlightedText As _
New SolidBrush(System.Drawing.SystemColors.HighlightText)
e.Graphics.DrawString(obj.ToString, e.Font, _
HighlightedText, rec)
'If the item starts with EXPIRED make it red
ElseIf obj.ToString.StartsWith("EXPIRED") Then
Dim ExpireText As New SolidBrush(System.Drawing.Color.Red)
e.Graphics.DrawString(obj.ToString, e.Font, ExpireText, rec)
Else
Dim NormalText As New _
SolidBrush(System.Drawing.SystemColors.ControlText)
e.Graphics.DrawString(obj.ToString, e.Font, NormalText, rec)
End If
Else
Dim NormalText As New _
SolidBrush(System.Drawing.SystemColors.ControlText)
e.Graphics.DrawString("", e.Font, NormalText, rec)
End If
End Sub
End Class
There is not a lot of code to this technique, and it could be easily applied to other canned History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||