目的:實現如下圖所示效果:
寫實現進度條的類,繼承DataGridViewTextBoxColumn類,代碼如下:
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class DataGridViewPrassBar
Public Class DataGridViewProgressBarColumn
Inherits DataGridViewTextBoxColumn
Public Sub New()
Me.CellTemplate =
New DataGridViewProgressBarCell()
End SubPublic Overrides Property CellTemplate()
As DataGridViewCell
GetReturn MyBase.CellTemplate
End GetSet(
ByVal value
As DataGridViewCell)
If Not TypeOf value
Is DataGridViewProgressBarCell
ThenThrow New InvalidCastException(
"請指定DataGridViewProgressBarCell")
End IfMyBase.CellTemplate = value
End SetEnd PropertyPublic Property Maximum()
As IntegerGetReturn CType(
Me.CellTemplate, DataGridViewProgressBarCell).Maximum
End GetSet(
ByVal value
As Integer)
If Me.Maximum = value
ThenReturnEnd IfCType(
Me.CellTemplate, DataGridViewProgressBarCell).Maximum = value
If Me.DataGridView
Is Nothing ThenReturnEnd IfDim rowCount
As Integer =
Me.DataGridView.RowCount
Dim i
As IntegerFor i =
0 To rowCount -
1Dim r
As DataGridViewRow =
Me.DataGridView.Rows.SharedRow(i)
CType(r.Cells(
Me.Index), DataGridViewProgressBarCell).Maximum = value
Next i
End SetEnd PropertyPublic Property Mimimum()
As IntegerGetReturn CType(
Me.CellTemplate, DataGridViewProgressBarCell).Mimimum
End GetSet(
ByVal value
As Integer)
If Me.Mimimum = value
ThenReturnEnd IfCType(
Me.CellTemplate, DataGridViewProgressBarCell).Mimimum = value
If Me.DataGridView
Is Nothing ThenReturnEnd IfDim rowCount
As Integer =
Me.DataGridView.RowCount
Dim i
As IntegerFor i =
0 To rowCount -
1Dim r
As DataGridViewRow =
Me.DataGridView.Rows.SharedRow(i)
CType(r.Cells(
Me.Index), DataGridViewProgressBarCell).Mimimum = value
Next i
End SetEnd PropertyEnd ClassPublic Class DataGridViewProgressBarCell
Inherits DataGridViewTextBoxCell
Public Sub New()
Me.maximumValue =
100Me.mimimumValue =
0End SubPrivate maximumValue
As IntegerPublic Property Maximum()
As IntegerGetReturn Me.maximumValue
End GetSet(
ByVal value
As Integer)
Me.maximumValue = value
End SetEnd PropertyPrivate mimimumValue
As IntegerPublic Property Mimimum()
As IntegerGetReturn Me.mimimumValue
End GetSet(
ByVal value
As Integer)
Me.mimimumValue = value
End SetEnd PropertyPublic Overrides ReadOnly Property ValueType()
As Type
GetReturn GetType(
Integer)
End GetEnd PropertyPublic Overrides ReadOnly Property DefaultNewRowValue()
As ObjectGetReturn 0End GetEnd PropertyPublic Overrides Function Clone()
As ObjectDim cell
As DataGridViewProgressBarCell =
CType(
MyBase.Clone(), DataGridViewProgressBarCell)cell.Maximum =
Me.Maximumcell.Mimimum =
Me.Mimimum
Return cell
End FunctionProtected 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 =
0If TypeOf value
Is Integer ThenintValue =
CInt(value)
End IfIf intValue <
Me.mimimumValue
ThenintValue =
Me.mimimumValue
End IfIf intValue >
Me.maximumValue
ThenintValue =
Me.maximumValue
End IfDim rate
As Double =
CDbl(intValue -
Me.mimimumValue) / (
Me.maximumValue -
Me.mimimumValue)
If (paintParts
And DataGridViewPaintParts.Border) = DataGridViewPaintParts.Border
ThenMe.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)
End IfDim 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
ThenbkColor = cellStyle.SelectionBackColor
ElsebkColor = cellStyle.BackColor
End IfIf (paintParts
And DataGridViewPaintParts.Background) = DataGridViewPaintParts.Background
ThenDim backBrush
As New SolidBrush(bkColor)
Trygraphics.FillRectangle(backBrush, paintRect)
FinallybackBrush.Dispose()
End TryEnd IfpaintRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top)paintRect.Width -= cellStyle.Padding.HorizontalpaintRect.Height -= cellStyle.Padding.Vertical
If (paintParts
And DataGridViewPaintParts.ContentForeground) = DataGridViewPaintParts.ContentForeground
ThenIf ProgressBarRenderer.IsSupported
ThenProgressBarRenderer.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)
Elsegraphics.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 IfEnd IfIf Me.DataGridView.CurrentCellAddress.X =
Me.ColumnIndex
AndAlso Me.DataGridView.CurrentCellAddress.Y =
Me.RowIndex
AndAlso (paintParts
And DataGridViewPaintParts.Focus) = DataGridViewPaintParts.Focus
AndAlso Me.DataGridView.Focused
ThenDim focusRect
As Rectangle = paintRectfocusRect.Inflate(-
3, -
3)ControlPaint.DrawFocusRectangle(graphics, focusRect)
End IfIf (paintParts
And DataGridViewPaintParts.ContentForeground) = DataGridViewPaintParts.ContentForeground
ThenDim txt
As String =
String.Format(
"{0}%", Math.Round((rate *
100)))
Dim flags
As TextFormatFlags = TextFormatFlags.HorizontalCenter
Or TextFormatFlags.VerticalCenter
Dim fColor
As Color = cellStyle.ForeColorpaintRect.Inflate(-
2, -
2)TextRenderer.DrawText(graphics, txt, cellStyle.Font, paintRect, fColor, flags)
End IfIf (paintParts
And DataGridViewPaintParts.ErrorIcon) = DataGridViewPaintParts.ErrorIcon
AndAlso Me.DataGridView.ShowCellErrors
AndAlso Not String.IsNullOrEmpty(errorText)
ThenDim iconBounds
As Rectangle =
Me.GetErrorIconBounds(graphics, cellStyle, rowIndex)iconBounds.Offset(cellBounds.X, cellBounds.Y)
Me.PaintErrorIcon(graphics, iconBounds, cellBounds, errorText)
End IfEnd SubEnd Class
End Class
運行項目進行編譯
對DataGridView進行列編輯,選中要設置進度條顯示的列,把columnType設置為自己寫的DataGridViewProgressBarColumn,默認的是DataGridViewTextBoxColumn屬性。可以定義好最大值和最小值,默認的是0,100.把value值賦值成介于最大值和最小值之間的整數就可以顯示進度條的效果了。
如果不正常顯示的話,檢查是不是正整數值,另外不要把賦值符號“=”右邊的值寫成計算的形式,要把計算好的值直接賦予value值。
總結
以上是生活随笔為你收集整理的VB中添加进度条列的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。