Creiamo una classe chiamata DataGridViewProgressCell
che si occuperà di disegnare la progressbar ed eredita dalla classe DataGridViewImageCell, ed una classe DataGridViewProgressColumn che eredita dalla classe DataGridViewImageColumn.
DataGridViewProgressCell.vb
Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel
Namespace Morpheusweb.Controls
Public Class DataGridViewProgressCell Inherits DataGridViewImageCell
Private _maxValue As Single = 100
Private _fillingColor As Color = Color.FromArgb(21, 232, 30)
Private _drawText As Boolean
Public Property DrawText() As Boolean
Get
Return _drawText
End Get
Set
_drawText = value
End Set
End Property
Public Property FillingColor() As Color
Get
Return _fillingColor
End Get
Set
_fillingColor = value
End Set
End Property
Public Property MaxValue() As Single
Get
Return _maxValue
End Get
Set
_maxValue = value
End Set
End Property
Private Shared emptyImage As Image
Shared Sub New()
emptyImage = New Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
End Sub
Public Sub New()
ValueType = GetType(Integer)
End Sub
Public Overrides NotOverridable Property ValueType() As Type
Get
Return MyBase.ValueType
End Get
Set
MyBase.ValueType = value
End Set
End Property
Protected Overrides Function GetFormattedValue(value As Object, rowIndex As Integer,
ByRef cellStyle As DataGridViewCellStyle, valueTypeConverter As TypeConverter,
formattedValueTypeConverter As TypeConverter,
context As DataGridViewDataErrorContexts) As Object
Return emptyImage
End Function
Protected Overrides Sub Paint(g As Graphics, clipBounds As Rectangle,
cellBounds As Rectangle, rowIndex As Integer,
cellState As DataGridViewElementStates, value As Object, _
formattedValue As Object, errorText As String, cellStyle As DataGridViewCellStyle,
advancedBorderStyle As DataGridViewAdvancedBorderStyle, paintParts As DataGridViewPaintParts)
[..]
End Sub
End Class
End Namespace
DataGridViewProgressColumn.vb
Imports System.Windows.Forms
Imports System.Drawing
Namespace Morpheusweb.Controls
Public Class DataGridViewProgressColumn Inherits DataGridViewImageColumn
Private _maxValue As Single
Private _fillingColor As Color = Color.Green
Private _drawText As Boolean
Public Property DrawText() As Boolean
Get
Return _drawText
End Get
Set
_drawText = value
End Set
End Property
Public Property FillingColor() As Color
Get
Return _fillingColor
End Get
Set
_fillingColor = value
End Set
End Property
Public Property MaxValue() As Single
Get
Return _maxValue
End Get
Set
_maxValue = value
End Set
End Property
Public Sub New()
CellTemplate = New DataGridViewProgressCell()
DirectCast(CellTemplate, DataGridViewProgressCell).FillingColor = _fillingColor
DirectCast(CellTemplate, DataGridViewProgressCell).MaxValue = _maxValue
DirectCast(CellTemplate, DataGridViewProgressCell).DrawText = _drawText
End Sub
Public Overrides NotOverridable Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set
MyBase.CellTemplate = value
End Set
End Property
End Class
End Namespace
Infine usiamo le classi come nell'esempio in basso:
Occorre creare una colonna di tipo DataGridViewProgressColumn e valorizzarne i dati.
'inizializzazione:
Dim col As New DataGridViewProgressColumn()
col.Name = "Progress"
col.HeaderText = ""
col.Width = 360
grdDati.Columns.Add(col)
'progress:
Dim progressCell As DataGridViewProgressCell = TryCast(Row.Cells("Progress"), DataGridViewProgressCell)
progressCell.MaxValue = 10
For i As Integer = 1 To 10
progressCell.Value = i
Next