这是DataGridViewProgressColumn 的示例类,基本上应该正是您想要实现的目标:
Imports System.Drawing
Imports System.ComponentModel
Public Class DataGridViewProgressColumn
Inherits DataGridViewImageColumn
Public Sub New()
Me.CellTemplate = New DataGridViewProgressCell
End Sub
End Class
Public Class DataGridViewProgressCell
Inherits DataGridViewImageCell
Sub New()
ValueType = Type.GetType("Integer")
End Sub
' Method required to make the Progress Cell consistent with the default Image Cell.
' The default Image Cell assumes an Image as a value, although the value of the Progress Cell is an Integer.
Protected Overrides Function GetFormattedValue( _
ByVal value As Object, _
ByVal rowIndex As Integer, _
ByRef cellStyle As DataGridViewCellStyle, _
ByVal valueTypeConverter As TypeConverter, _
ByVal formattedValueTypeConverter As TypeConverter, _
ByVal context As DataGridViewDataErrorContexts _
) As Object
Static emptyImage As Bitmap = New Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
GetFormattedValue = emptyImage
End Function
Protected Overrides Sub Paint(ByVal g 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)
Try
Dim progressVal As Integer = CType(IIf(IsDBNull(value), 0, value), Integer)
Dim percentage As Single = CType((progressVal / 100), Single)
Dim backBrush As Brush = New SolidBrush(cellStyle.BackColor)
Dim foreBrush As Brush = New SolidBrush(cellStyle.ForeColor)
' Call the base class method to paint the default cell appearance.
MyBase.Paint(g, clipBounds, cellBounds, rowIndex, cellState, _
value, formattedValue, errorText, cellStyle, _
advancedBorderStyle, paintParts)
If percentage > 0.0 Then
' Draw the progress bar and the text
g.FillRectangle(New SolidBrush(Color.FromArgb(163, 189, 242)), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 4)), cellBounds.Height - 4)
g.DrawString(progressVal.ToString() & "%", cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
Else
'draw the text
If Not Me.DataGridView.CurrentCell Is Nothing AndAlso Me.DataGridView.CurrentCell.RowIndex = rowIndex Then
g.DrawString(progressVal.ToString() & "%", cellStyle.Font, New SolidBrush(cellStyle.SelectionForeColor), cellBounds.X + 6, cellBounds.Y + 2)
Else
g.DrawString(progressVal.ToString() & "%", cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
End If
End If
Catch ex As Exception
MsgBox("Graf nemohl být načten.")
End Try
End Sub
End Class
定义您的 Columns 并将它们添加到 DataGridView(您也可以在 GUI 中执行此操作!):
Dim WIcol0 As New DataGridViewTextBoxColumn
Dim WIcol1 As New DataGridViewTextBoxColumn
Dim WIcol2 As New DataGridViewTextBoxColumn
Dim WIcol3 As New DataGridViewTextBoxColumn
Dim WIcol4 As New DataGridViewTextBoxColumn
Dim WIcol4a As New DataGridViewProgressColumn ' <<< Define a column of your type
With WIcol0
.Width = "45"
.HeaderText = "#"
.DataPropertyName = "RowNo"
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
.DefaultCellStyle.Padding = New Padding(6, 0, 0, 0)
.Name = .DataPropertyName
.Visible = True
.ReadOnly = True
End With
With WIcol1
.Width = "250"
.HeaderText = "Company Name"
.DataPropertyName = "CompanyName"
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
.DefaultCellStyle.Padding = New Padding(6, 0, 0, 0)
.Name = .DataPropertyName
.Visible = True
.ReadOnly = True
End With
With WIcol2
.Width = "100"
.HeaderText = "Turnover"
.DataPropertyName = "Turnover"
.DefaultCellStyle.Format = "# ### ##0.00 Kč"
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
.Name = .DataPropertyName
.ReadOnly = True
End With
With WIcol3
.Width = "80"
.HeaderText = "Running Time"
.DataPropertyName = "RunningTime"
.DefaultCellStyle.Format = "0 dní"
'.DefaultCellStyle.Padding = New Padding(6, 0, 0, 0)
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
.Name = .DataPropertyName
.ReadOnly = True
End With
With WIcol4
.Width = "80"
.HeaderText = "After"
.DataPropertyName = "After"
.DefaultCellStyle.Format = "0.0 dní"
.DefaultCellStyle.Font = New Font(Me.dgwOTD.Font, FontStyle.Bold)
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
.Name = .DataPropertyName
.ReadOnly = True
End With
With WIcol4a
.Width = "80"
.HeaderText = "Performance"
.DataPropertyName = "Performance"
'.DefaultCellStyle.Format = "0 dní"
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
.DefaultCellStyle.ForeColor = Color.LightGray
.Name = .DataPropertyName
.DefaultCellStyle.Format = ""
.ReadOnly = True
End With
编辑:我刚刚意识到它是 VB.NET 并且你有 C#,但逻辑当然是相同的。如果您阅读有困难,请发表评论,我会附上翻译后的 C# 代码。