- Код: Выделить всё
Public Class Form1
Public Class DataGridViewProgressBarColumn
Inherits DataGridViewColumn
Sub New()
MyBase.New(New DataGridViewProgressBarCell)
End Sub
Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)
If Not (value Is Nothing) And Not value.GetType().IsAssignableFrom(GetType(DataGridViewProgressBarCell)) Then
Throw New InvalidCastException("Must be a DataGridViewProgressBarCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
Public Class DataGridViewProgressBarCell
Inherits DataGridViewCell
Private ProgressBar1 As ProgressBar
Sub New()
MyBase.New()
ProgressBar1 = New ProgressBar
End Sub
Public Overrides ReadOnly Property FormattedValueType() As System.Type
Get
Return GetType(String)
End Get
End Property
Protected Overrides Sub Paint(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal cellBounds As System.Drawing.Rectangle, ByVal rowIndex As Integer, ByVal cellState As System.Windows.Forms.DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle, ByVal paintParts As System.Windows.Forms.DataGridViewPaintParts)
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
Dim gr As Graphics = graphics
If (cellState And DataGridViewElementStates.Selected) = DataGridViewElementStates.Selected Then
gr.FillRectangle(SystemBrushes.Highlight, cellBounds)
Else
gr.FillRectangle(Brushes.White, cellBounds)
End If
'Disegna i bordi
gr.DrawLine(Pens.Gray, cellBounds.Right - 1, cellBounds.Top, cellBounds.Right - 1, cellBounds.Bottom)
gr.DrawLine(Pens.Gray, cellBounds.Left, cellBounds.Bottom - 1, cellBounds.Right, cellBounds.Bottom - 1)
gr.DrawLine(Pens.White, cellBounds.Right - 2, cellBounds.Top, cellBounds.Right - 2, cellBounds.Bottom - 2)
gr.DrawLine(Pens.White, cellBounds.Left, cellBounds.Bottom - 2, cellBounds.Right - 2, cellBounds.Bottom - 2)
gr.DrawLine(SystemPens.ControlDark, cellBounds.Left, cellBounds.Top, cellBounds.Right - 3, cellBounds.Top)
gr.DrawLine(SystemPens.ControlDark, cellBounds.Left, cellBounds.Top, cellBounds.Left, cellBounds.Bottom - 3)
gr.DrawLine(SystemPens.ControlDarkDark, cellBounds.Left + 1, cellBounds.Top + 1, cellBounds.Right - 4, cellBounds.Top + 1)
gr.DrawLine(SystemPens.ControlDarkDark, cellBounds.Left + 1, cellBounds.Top + 1, cellBounds.Left + 1, cellBounds.Bottom - 4)
gr.DrawLine(SystemPens.Control, cellBounds.Left + 1, cellBounds.Bottom - 3, cellBounds.Right - 3, cellBounds.Bottom - 3)
gr.DrawLine(SystemPens.Control, cellBounds.Right - 3, cellBounds.Top + 1, cellBounds.Right - 3, cellBounds.Bottom - 3)
DrawProgressBar(gr, New Rectangle(cellBounds.X + 3, cellBounds.Y + 3, cellBounds.Width - 7, cellBounds.Height - 7), value, cellState, cellStyle)
End Sub
Private Sub DrawProgressBar(ByVal gr As Graphics, ByVal bounds As Rectangle, ByVal Value As Object, ByVal cellState As System.Windows.Forms.DataGridViewElementStates, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle)
If Not Value Is Nothing Then
Dim Width As Single = (bounds.Width / 100) * Value
Dim Bar As Rectangle = New Rectangle(bounds.X, bounds.Y, Width, bounds.Height)
Dim barBrush As Brush
Dim TextBrush As Brush
If (cellState And DataGridViewElementStates.Selected) = DataGridViewElementStates.Selected Then
barBrush = Brushes.White
TextBrush = Brushes.Black
Else
barBrush = SystemBrushes.Highlight
TextBrush = Brushes.Black
End If
gr.FillRectangle(barBrush, Bar)
Dim s As SizeF = gr.MeasureString(Value.ToString & "%", cellStyle.Font)
gr.DrawString(Value.ToString & "%", cellStyle.Font, TextBrush, bounds.X + (bounds.Width - s.Width) / 2, bounds.Top + 1)
End If
End Sub
End Class
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Grid.RowCount = 5
Grid.ColumnCount = 5
Dim V As New DataGridViewProgressBarCell
V.Value = 45
Grid.Item(0, 0) = V
End Sub
End Class