请选择 进入手机版 | 继续访问电脑版
查看: 246|回复: 0

[.NET开发] VB.NET Datagridview 增加列用来显示进度条

3万

主题

3万

帖子

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
100197
发表于 2015-11-23 17:52:04

这段代码演示了在DataGridView中增加一个列显示进度条,在批量上传和下载的时候常用到。

进度条的变化如: DataGridview.row(x).cell(x).value=50
改变他的值就是了。

  1. Imports System
  2. Imports System.Drawing
  3. Imports System.Windows.Forms
  4. Public Class DataGridViewPrassBar
  5. Public Class DataGridViewProgressBarColumn
  6. Inherits DataGridViewTextBoxColumn
  7. Public Sub New()
  8. Me.CellTemplate = New DataGridViewProgressBarCell()
  9. End Sub
  10. Public Overrides Property CellTemplate() As DataGridViewCell
  11. Get
  12. Return MyBase.CellTemplate
  13. End Get
  14. Set(ByVal value As DataGridViewCell)
  15. If Not TypeOf value Is DataGridViewProgressBarCell Then
  16. Throw New InvalidCastException("DataGridViewProgressBarCellオブジェクトを" + "指定してください。")
  17. End If
  18. MyBase.CellTemplate = value
  19. End Set
  20. End Property
  21. Public Property Maximum() As Integer
  22. Get
  23. Return CType(Me.CellTemplate, DataGridViewProgressBarCell).Maximum
  24. End Get
  25. Set(ByVal value As Integer)
  26. If Me.Maximum = value Then
  27. Return
  28. End If
  29. CType(Me.CellTemplate, DataGridViewProgressBarCell).Maximum = value
  30. If Me.DataGridView Is Nothing Then
  31. Return
  32. End If
  33. Dim rowCount As Integer = Me.DataGridView.RowCount
  34. Dim i As Integer
  35. For i = 0 To rowCount - 1
  36. Dim r As DataGridViewRow = Me.DataGridView.Rows.SharedRow(i)
  37. CType(r.Cells(Me.Index), DataGridViewProgressBarCell).Maximum = value
  38. Next i
  39. End Set
  40. End Property
  41. Public Property Mimimum() As Integer
  42. Get
  43. Return CType(Me.CellTemplate, DataGridViewProgressBarCell).Mimimum
  44. End Get
  45. Set(ByVal value As Integer)
  46. If Me.Mimimum = value Then
  47. Return
  48. End If
  49. CType(Me.CellTemplate, DataGridViewProgressBarCell).Mimimum = value
  50. If Me.DataGridView Is Nothing Then
  51. Return
  52. End If
  53. Dim rowCount As Integer = Me.DataGridView.RowCount
  54. Dim i As Integer
  55. For i = 0 To rowCount - 1
  56. Dim r As DataGridViewRow = Me.DataGridView.Rows.SharedRow(i)
  57. CType(r.Cells(Me.Index), DataGridViewProgressBarCell).Mimimum = value
  58. Next i
  59. End Set
  60. End Property
  61. End Class
  62. Public Class DataGridViewProgressBarCell
  63. Inherits DataGridViewTextBoxCell
  64. Public Sub New()
  65. Me.maximumValue = 100
  66. Me.mimimumValue = 0
  67. End Sub
  68. Private maximumValue As Integer
  69. Public Property Maximum() As Integer
  70. Get
  71. Return Me.maximumValue
  72. End Get
  73. Set(ByVal value As Integer)
  74. Me.maximumValue = value
  75. End Set
  76. End Property
  77. Private mimimumValue As Integer
  78. Public Property Mimimum() As Integer
  79. Get
  80. Return Me.mimimumValue
  81. End Get
  82. Set(ByVal value As Integer)
  83. Me.mimimumValue = value
  84. End Set
  85. End Property
  86. Public Overrides ReadOnly Property ValueType() As Type
  87. Get
  88. Return GetType(Integer)
  89. End Get
  90. End Property
  91. Public Overrides ReadOnly Property DefaultNewRowValue() As Object
  92. Get
  93. Return 0
  94. End Get
  95. End Property
  96. Public Overrides Function Clone() As Object
  97. Dim cell As DataGridViewProgressBarCell = CType(MyBase.Clone(), DataGridViewProgressBarCell)
  98. cell.Maximum = Me.Maximum
  99. cell.Mimimum = Me.Mimimum
  100. Return cell
  101. End Function
  102. 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)
  103. Dim intValue As Integer = 0
  104. If TypeOf value Is Integer Then
  105. intValue = CInt(value)
  106. End If
  107. If intValue < Me.mimimumValue Then
  108. intValue = Me.mimimumValue
  109. End If
  110. If intValue > Me.maximumValue Then
  111. intValue = Me.maximumValue
  112. End If
  113. Dim rate As Double = CDbl(intValue - Me.mimimumValue) / (Me.maximumValue - Me.mimimumValue)
  114. If (paintParts And DataGridViewPaintParts.Border) = DataGridViewPaintParts.Border Then
  115. Me.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)
  116. End If
  117. Dim borderRect As Rectangle = Me.BorderWidths(advancedBorderStyle)
  118. Dim paintRect As New Rectangle(cellBounds.Left + borderRect.Left, cellBounds.Top + borderRect.Top, cellBounds.Width - borderRect.Right, cellBounds.Height - borderRect.Bottom)
  119. Dim isSelected As Boolean = ((cellState And DataGridViewElementStates.Selected) = DataGridViewElementStates.Selected)
  120. Dim bkColor As Color
  121. If isSelected AndAlso (paintParts And DataGridViewPaintParts.SelectionBackground) = DataGridViewPaintParts.SelectionBackground Then
  122. bkColor = cellStyle.SelectionBackColor
  123. Else
  124. bkColor = cellStyle.BackColor
  125. End If
  126. If (paintParts And DataGridViewPaintParts.Background) = DataGridViewPaintParts.Background Then
  127. Dim backBrush As New SolidBrush(bkColor)
  128. Try
  129. graphics.FillRectangle(backBrush, paintRect)
  130. Finally
  131. backBrush.Dispose()
  132. End Try
  133. End If
  134. paintRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top)
  135. paintRect.Width -= cellStyle.Padding.Horizontal
  136. paintRect.Height -= cellStyle.Padding.Vertical
  137. If (paintParts And DataGridViewPaintParts.ContentForeground) = DataGridViewPaintParts.ContentForeground Then
  138. If ProgressBarRenderer.IsSupported Then
  139. ProgressBarRenderer.DrawHorizontalBar(graphics, paintRect)
  140. Dim barBounds As New Rectangle(paintRect.Left + 3, paintRect.Top + 3, paintRect.Width - 4, paintRect.Height - 6)
  141. barBounds.Width = CInt(Math.Round((barBounds.Width * rate)))
  142. ProgressBarRenderer.DrawHorizontalChunks(graphics, barBounds)
  143. Else
  144. graphics.FillRectangle(Brushes.White, paintRect)
  145. graphics.DrawRectangle(Pens.Black, paintRect)
  146. Dim barBounds As New Rectangle(paintRect.Left + 1, paintRect.Top + 1, paintRect.Width - 1, paintRect.Height - 1)
  147. barBounds.Width = CInt(Math.Round((barBounds.Width * rate)))
  148. graphics.FillRectangle(Brushes.Blue, barBounds)
  149. End If
  150. End If
  151. 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
  152. Dim focusRect As Rectangle = paintRect
  153. focusRect.Inflate(-3, -3)
  154. ControlPaint.DrawFocusRectangle(graphics, focusRect)
  155. End If
  156. If (paintParts And DataGridViewPaintParts.ContentForeground) = DataGridViewPaintParts.ContentForeground Then
  157. Dim txt As String = String.Format("{0}%", Math.Round((rate * 100)))
  158. Dim flags As TextFormatFlags = TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter
  159. Dim fColor As Color = cellStyle.ForeColor
  160. paintRect.Inflate(-2, -2)
  161. TextRenderer.DrawText(graphics, txt, cellStyle.Font, paintRect, fColor, flags)
  162. End If
  163. If (paintParts And DataGridViewPaintParts.ErrorIcon) = DataGridViewPaintParts.ErrorIcon AndAlso Me.DataGridView.ShowCellErrors AndAlso Not String.IsNullOrEmpty(errorText) Then
  164. Dim iconBounds As Rectangle = Me.GetErrorIconBounds(graphics, cellStyle, rowIndex)
  165. iconBounds.Offset(cellBounds.X, cellBounds.Y)
  166. Me.PaintErrorIcon(graphics, iconBounds, cellBounds, errorText)
  167. End If
  168. End Sub
  169. End Class
  170. End Class
  171. 调用:
  172. Dim pbColumn As New DataGridViewPrassBar.DataGridViewProgressBarColumn()
  173. pbColumn.DataPropertyName = "Column1"
  174. DataGridView1.Columns.Add(pbColumn)
复制代码
  1. Imports System
  2. Imports System.Drawing
  3. Imports System.Windows.Forms
  4. Public Class DataGridViewPrassBar
  5. Public Class DataGridViewProgressBarColumn
  6. Inherits DataGridViewTextBoxColumn
  7. Public Sub New()
  8. Me.CellTemplate = New DataGridViewProgressBarCell()
  9. End Sub
  10. Public Overrides Property CellTemplate() As DataGridViewCell
  11. Get
  12. Return MyBase.CellTemplate
  13. End Get
  14. Set(ByVal value As DataGridViewCell)
  15. If Not TypeOf value Is DataGridViewProgressBarCell Then
  16. Throw New InvalidCastException("DataGridViewProgressBarCellオブジェクトを" + "指定してください。")
  17. End If
  18. MyBase.CellTemplate = value
  19. End Set
  20. End Property
  21. Public Property Maximum() As Integer
  22. Get
  23. Return CType(Me.CellTemplate, DataGridViewProgressBarCell).Maximum
  24. End Get
  25. Set(ByVal value As Integer)
  26. If Me.Maximum = value Then
  27. Return
  28. End If
  29. CType(Me.CellTemplate, DataGridViewProgressBarCell).Maximum = value
  30. If Me.DataGridView Is Nothing Then
  31. Return
  32. End If
  33. Dim rowCount As Integer = Me.DataGridView.RowCount
  34. Dim i As Integer
  35. For i = 0 To rowCount - 1
  36. Dim r As DataGridViewRow = Me.DataGridView.Rows.SharedRow(i)
  37. CType(r.Cells(Me.Index), DataGridViewProgressBarCell).Maximum = value
  38. Next i
  39. End Set
  40. End Property
  41. Public Property Mimimum() As Integer
  42. Get
  43. Return CType(Me.CellTemplate, DataGridViewProgressBarCell).Mimimum
  44. End Get
  45. Set(ByVal value As Integer)
  46. If Me.Mimimum = value Then
  47. Return
  48. End If
  49. CType(Me.CellTemplate, DataGridViewProgressBarCell).Mimimum = value
  50. If Me.DataGridView Is Nothing Then
  51. Return
  52. End If
  53. Dim rowCount As Integer = Me.DataGridView.RowCount
  54. Dim i As Integer
  55. For i = 0 To rowCount - 1
  56. Dim r As DataGridViewRow = Me.DataGridView.Rows.SharedRow(i)
  57. CType(r.Cells(Me.Index), DataGridViewProgressBarCell).Mimimum = value
  58. Next i
  59. End Set
  60. End Property
  61. End Class
  62. Public Class DataGridViewProgressBarCell
  63. Inherits DataGridViewTextBoxCell
  64. Public Sub New()
  65. Me.maximumValue = 100
  66. Me.mimimumValue = 0
  67. End Sub
  68. Private maximumValue As Integer
  69. Public Property Maximum() As Integer
  70. Get
  71. Return Me.maximumValue
  72. End Get
  73. Set(ByVal value As Integer)
  74. Me.maximumValue = value
  75. End Set
  76. End Property
  77. Private mimimumValue As Integer
  78. Public Property Mimimum() As Integer
  79. Get
  80. Return Me.mimimumValue
  81. End Get
  82. Set(ByVal value As Integer)
  83. Me.mimimumValue = value
  84. End Set
  85. End Property
  86. Public Overrides ReadOnly Property ValueType() As Type
  87. Get
  88. Return GetType(Integer)
  89. End Get
  90. End Property
  91. Public Overrides ReadOnly Property DefaultNewRowValue() As Object
  92. Get
  93. Return 0
  94. End Get
  95. End Property
  96. Public Overrides Function Clone() As Object
  97. Dim cell As DataGridViewProgressBarCell = CType(MyBase.Clone(), DataGridViewProgressBarCell)
  98. cell.Maximum = Me.Maximum
  99. cell.Mimimum = Me.Mimimum
  100. Return cell
  101. End Function
  102. 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)
  103. Dim intValue As Integer = 0
  104. If TypeOf value Is Integer Then
  105. intValue = CInt(value)
  106. End If
  107. If intValue < Me.mimimumValue Then
  108. intValue = Me.mimimumValue
  109. End If
  110. If intValue > Me.maximumValue Then
  111. intValue = Me.maximumValue
  112. End If
  113. Dim rate As Double = CDbl(intValue - Me.mimimumValue) / (Me.maximumValue - Me.mimimumValue)
  114. If (paintParts And DataGridViewPaintParts.Border) = DataGridViewPaintParts.Border Then
  115. Me.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)
  116. End If
  117. Dim borderRect As Rectangle = Me.BorderWidths(advancedBorderStyle)
  118. Dim paintRect As New Rectangle(cellBounds.Left + borderRect.Left, cellBounds.Top + borderRect.Top, cellBounds.Width - borderRect.Right, cellBounds.Height - borderRect.Bottom)
  119. Dim isSelected As Boolean = ((cellState And DataGridViewElementStates.Selected) = DataGridViewElementStates.Selected)
  120. Dim bkColor As Color
  121. If isSelected AndAlso (paintParts And DataGridViewPaintParts.SelectionBackground) = DataGridViewPaintParts.SelectionBackground Then
  122. bkColor = cellStyle.SelectionBackColor
  123. Else
  124. bkColor = cellStyle.BackColor
  125. End If
  126. If (paintParts And DataGridViewPaintParts.Background) = DataGridViewPaintParts.Background Then
  127. Dim backBrush As New SolidBrush(bkColor)
  128. Try
  129. graphics.FillRectangle(backBrush, paintRect)
  130. Finally
  131. backBrush.Dispose()
  132. End Try
  133. End If
  134. paintRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top)
  135. paintRect.Width -= cellStyle.Padding.Horizontal
  136. paintRect.Height -= cellStyle.Padding.Vertical
  137. If (paintParts And DataGridViewPaintParts.ContentForeground) = DataGridViewPaintParts.ContentForeground Then
  138. If ProgressBarRenderer.IsSupported Then
  139. ProgressBarRenderer.DrawHorizontalBar(graphics, paintRect)
  140. Dim barBounds As New Rectangle(paintRect.Left + 3, paintRect.Top + 3, paintRect.Width - 4, paintRect.Height - 6)
  141. barBounds.Width = CInt(Math.Round((barBounds.Width * rate)))
  142. ProgressBarRenderer.DrawHorizontalChunks(graphics, barBounds)
  143. Else
  144. graphics.FillRectangle(Brushes.White, paintRect)
  145. graphics.DrawRectangle(Pens.Black, paintRect)
  146. Dim barBounds As New Rectangle(paintRect.Left + 1, paintRect.Top + 1, paintRect.Width - 1, paintRect.Height - 1)
  147. barBounds.Width = CInt(Math.Round((barBounds.Width * rate)))
  148. graphics.FillRectangle(Brushes.Blue, barBounds)
  149. End If
  150. End If
  151. 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
  152. Dim focusRect As Rectangle = paintRect
  153. focusRect.Inflate(-3, -3)
  154. ControlPaint.DrawFocusRectangle(graphics, focusRect)
  155. End If
  156. If (paintParts And DataGridViewPaintParts.ContentForeground) = DataGridViewPaintParts.ContentForeground Then
  157. Dim txt As String = String.Format("{0}%", Math.Round((rate * 100)))
  158. Dim flags As TextFormatFlags = TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter
  159. Dim fColor As Color = cellStyle.ForeColor
  160. paintRect.Inflate(-2, -2)
  161. TextRenderer.DrawText(graphics, txt, cellStyle.Font, paintRect, fColor, flags)
  162. End If
  163. If (paintParts And DataGridViewPaintParts.ErrorIcon) = DataGridViewPaintParts.ErrorIcon AndAlso Me.DataGridView.ShowCellErrors AndAlso Not String.IsNullOrEmpty(errorText) Then
  164. Dim iconBounds As Rectangle = Me.GetErrorIconBounds(graphics, cellStyle, rowIndex)
  165. iconBounds.Offset(cellBounds.X, cellBounds.Y)
  166. Me.PaintErrorIcon(graphics, iconBounds, cellBounds, errorText)
  167. End If
  168. End Sub
  169. End Class
  170. End Class
  171. 调用:
  172. Dim pbColumn As New DataGridViewPrassBar.DataGridViewProgressBarColumn()
  173. pbColumn.DataPropertyName = "Column1"
  174. DataGridView1.Columns.Add(pbColumn)
复制代码


回复

使用道具 举报