VB.NET DataGridView添加进度条列

'DataGridView 添加进度条列的类
'Harry.Li 2016/10/01
'Edit LastTime 2016/10/01 23:25

'*******************************************
'将下面 DataGridViewPrassBar 类,整体放入模块中
'*******************************************
''' <summary>
''' DataGridView 添加进度条列的类
''' </summary>
''' <remarks></remarks>
Public Class DataGridViewPrassBar
    Public Class DataGridViewProgressBarColumn
        Inherits DataGridViewTextBoxColumn

        Public Sub New()
            Me.CellTemplate = New DataGridViewProgressBarCell()
        End Sub

        Public Overrides Property CellTemplate() As DataGridViewCell
            Get
                Return MyBase.CellTemplate
            End Get
            Set(ByVal value As DataGridViewCell)
                If Not TypeOf value Is DataGridViewProgressBarCell Then
                    Throw New InvalidCastException("DataGridViewProgressBarCellオブジェクトを" + "指定してください。")
                End If
                MyBase.CellTemplate = value
            End Set

        End Property

        Public Property Maximum() As Integer
            Get
                Return CType(Me.CellTemplate, DataGridViewProgressBarCell).Maximum
            End Get
            Set(ByVal value As Integer)
                If Me.Maximum = value Then
                    Return
                End If
                CType(Me.CellTemplate, DataGridViewProgressBarCell).Maximum = value
                If Me.DataGridView Is Nothing Then
                    Return
                End If
                Dim rowCount As Integer = Me.DataGridView.RowCount
                Dim i As Integer
                For i = 0 To rowCount - 1
                    Dim r As DataGridViewRow = Me.DataGridView.Rows.SharedRow(i)
                    CType(r.Cells(Me.Index), DataGridViewProgressBarCell).Maximum = value
                Next i
            End Set
        End Property

        Public Property Mimimum() As Integer
            Get
                Return CType(Me.CellTemplate, DataGridViewProgressBarCell).Mimimum
            End Get
            Set(ByVal value As Integer)

                If Me.Mimimum = value Then

                    Return

                End If

                CType(Me.CellTemplate, DataGridViewProgressBarCell).Mimimum = value

                If Me.DataGridView Is Nothing Then

                    Return

                End If
                Dim rowCount As Integer = Me.DataGridView.RowCount
                Dim i As Integer
                For i = 0 To rowCount - 1
                    Dim r As DataGridViewRow = Me.DataGridView.Rows.SharedRow(i)
                    CType(r.Cells(Me.Index), DataGridViewProgressBarCell).Mimimum = value
                Next i
            End Set
        End Property

    End Class

    Public Class DataGridViewProgressBarCell
        Inherits DataGridViewTextBoxCell

        Public Sub New()
            Me.maximumValue = 100
            Me.mimimumValue = 0
        End Sub
        Private maximumValue As Integer

        Public Property Maximum() As Integer
            Get
                Return Me.maximumValue
            End Get
            Set(ByVal value As Integer)
                Me.maximumValue = value
            End Set
        End Property

        Private mimimumValue As Integer

        Public Property Mimimum() As Integer
            Get
                Return Me.mimimumValue
            End Get
            Set(ByVal value As Integer)
                Me.mimimumValue = value
            End Set
        End Property

        Public Overrides ReadOnly Property ValueType() As Type
            Get
                Return GetType(Integer)
            End Get
        End Property

        Public Overrides ReadOnly Property DefaultNewRowValue() As Object
            Get
                Return 0
            End Get
        End Property

        Public Overrides Function Clone() As Object
            Dim cell As DataGridViewProgressBarCell = CType(MyBase.Clone(), DataGridViewProgressBarCell)
            cell.Maximum = Me.Maximum
            cell.Mimimum = Me.Mimimum
            Return cell
        End Function

        Protected Overrides Sub Paint(ByVal graphics As Graphics, ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, ByVal cellState As DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts)
            Dim intValue As Integer = 0
            If TypeOf value Is Integer Then
                intValue = CInt(value)
            End If
            If intValue < Me.mimimumValue Then
                intValue = Me.mimimumValue
            End If

            If intValue > Me.maximumValue Then
                intValue = Me.maximumValue
            End If

            Dim rate As Double = CDbl(intValue - Me.mimimumValue) / (Me.maximumValue - Me.mimimumValue)
            If (paintParts And DataGridViewPaintParts.Border) = DataGridViewPaintParts.Border Then
                Me.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)
            End If
            Dim borderRect As Rectangle = Me.BorderWidths(advancedBorderStyle)
            Dim paintRect As New Rectangle(cellBounds.Left + borderRect.Left, cellBounds.Top + borderRect.Top, cellBounds.Width - borderRect.Right, cellBounds.Height - borderRect.Bottom)
            Dim isSelected As Boolean = ((cellState And DataGridViewElementStates.Selected) = DataGridViewElementStates.Selected)
            Dim bkColor As Color
            If isSelected AndAlso (paintParts And DataGridViewPaintParts.SelectionBackground) = DataGridViewPaintParts.SelectionBackground Then
                bkColor = cellStyle.SelectionBackColor
            Else
                bkColor = cellStyle.BackColor
            End If
            If (paintParts And DataGridViewPaintParts.Background) = DataGridViewPaintParts.Background Then
                Dim backBrush As New SolidBrush(bkColor)
                Try
                    graphics.FillRectangle(backBrush, paintRect)
                Finally
                    backBrush.Dispose()
                End Try
            End If
            paintRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top)
            paintRect.Width -= cellStyle.Padding.Horizontal
            paintRect.Height -= cellStyle.Padding.Vertical
            If (paintParts And DataGridViewPaintParts.ContentForeground) = DataGridViewPaintParts.ContentForeground Then
                If ProgressBarRenderer.IsSupported Then
                    ProgressBarRenderer.DrawHorizontalBar(graphics, paintRect)
                    Dim barBounds As New Rectangle(paintRect.Left + 3, paintRect.Top + 3, paintRect.Width - 4, paintRect.Height - 6)
                    barBounds.Width = CInt(Math.Round((barBounds.Width * rate)))
                    ProgressBarRenderer.DrawHorizontalChunks(graphics, barBounds)
                Else
                    graphics.FillRectangle(Brushes.White, paintRect)
                    graphics.DrawRectangle(Pens.Black, paintRect)
                    Dim barBounds As New Rectangle(paintRect.Left + 1, paintRect.Top + 1, paintRect.Width - 1, paintRect.Height - 1)
                    barBounds.Width = CInt(Math.Round((barBounds.Width * rate)))
                    graphics.FillRectangle(Brushes.Blue, barBounds)
                End If
            End If
            If Me.DataGridView.CurrentCellAddress.X = Me.ColumnIndex AndAlso Me.DataGridView.CurrentCellAddress.Y = Me.RowIndex AndAlso (paintParts And DataGridViewPaintParts.Focus) = DataGridViewPaintParts.Focus AndAlso Me.DataGridView.Focused Then
                Dim focusRect As Rectangle = paintRect
                focusRect.Inflate(-3, -3)
                ControlPaint.DrawFocusRectangle(graphics, focusRect)
            End If
            If (paintParts And DataGridViewPaintParts.ContentForeground) = DataGridViewPaintParts.ContentForeground Then
                Dim txt As String = String.Format("{0}%", Math.Round((rate * 100)))
                Dim flags As TextFormatFlags = TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter
                Dim fColor As Color = cellStyle.ForeColor
                paintRect.Inflate(-2, -2)
                TextRenderer.DrawText(graphics, txt, cellStyle.Font, paintRect, fColor, flags)

            End If
            If (paintParts And DataGridViewPaintParts.ErrorIcon) = DataGridViewPaintParts.ErrorIcon AndAlso Me.DataGridView.ShowCellErrors AndAlso Not String.IsNullOrEmpty(errorText) Then
                Dim iconBounds As Rectangle = Me.GetErrorIconBounds(graphics, cellStyle, rowIndex)

                iconBounds.Offset(cellBounds.X, cellBounds.Y)

                Me.PaintErrorIcon(graphics, iconBounds, cellBounds, errorText)

            End If
        End Sub
    End Class
End Class

加载及调用方式

先为DataGridView对象定义进度条列

'定义进度条类列
Dim pbColumn As New DataGridViewPrassBar.DataGridViewProgressBarColumn()
pbColumn.DataPropertyName = "进度"
pbColumn.Maximum = 1000
pbColumn.Mimimum = 0
pbColumn.MinimumWidth = 10
DataGridView1.Columns.Add(0, "序号")
DataGridView1.Columns.Add(pbColumn)   '添加列

使用时可以直接给单元格赋值进行进度值设置

DataGridView1.Rows(i).Cells(j).Value = 20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值