VB.NET Datagridview 增加列用来显示进度条

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

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

标签 <无>

代码片段(4)

[代码] [ASP/Basic]代码

001 Imports System
002 Imports System.Drawing
003 Imports System.Windows.Forms
004 Public Class DataGridViewPrassBar
005 Public Class DataGridViewProgressBarColumn
006 Inherits DataGridViewTextBoxColumn
007
008 Public Sub New()
009 Me.CellTemplate = New DataGridViewProgressBarCell()
010 End Sub
011
012 Public Overrides Property CellTemplate() As DataGridViewCell
013 Get
014 Return MyBase.CellTemplate
015 End Get
016 Set(ByVal value As DataGridViewCell)
017 If Not TypeOf value Is DataGridViewProgressBarCell Then
018 Throw New InvalidCastException("DataGridViewProgressBarCellオブジェクトを" + "指定してください。")
019 End If
020 MyBase.CellTemplate = value
021 End Set
022
023 End Property
024
025
026
027 Public Property Maximum() As Integer
028 Get
029 Return CType(Me.CellTemplate,DataGridViewProgressBarCell).Maximum
030 End Get
031 Set(ByVal value As Integer)
032 If Me.Maximum = value Then
033 Return
034 End If
035 CType(Me.CellTemplate,DataGridViewProgressBarCell).Maximum = value
036 If Me.DataGridView Is nothing Then
037 Return
038 End If
039 Dim rowCount As Integer = Me.DataGridView.RowCount
040 Dim i As Integer
041 For i = 0 To rowCount - 1
042 Dim r As DataGridViewRow = Me.DataGridView.Rows.SharedRow(i)
043 CType(r.Cells(Me.Index),DataGridViewProgressBarCell).Maximum = value
044 Next i
045 End Set
046 End Property
047
048
049
050 Public Property Mimimum() As Integer
051 Get
052 Return CType(Me.CellTemplate,DataGridViewProgressBarCell).Mimimum
053 End Get
054 Set(ByVal value As Integer)
055
056 If Me.Mimimum = value Then
057
058 Return
059
060 End If
061
062 CType(Me.CellTemplate,DataGridViewProgressBarCell).Mimimum = value
063
064 If Me.DataGridView Is nothing Then
065
066 Return
067
068 End If
069 Dim rowCount As Integer = Me.DataGridView.RowCount
070 Dim i As Integer
071 For i = 0 To rowCount - 1
072 Dim r As DataGridViewRow = Me.DataGridView.Rows.SharedRow(i)
073 CType(r.Cells(Me.Index),DataGridViewProgressBarCell).Mimimum = value
074 Next i
075 End Set
076 End Property
077
078 End Class
079
080
081
082 Public Class DataGridViewProgressBarCell
083 Inherits DataGridViewTextBoxCell
084
085 Public Sub New()
086 Me.maximumValue = 100
087 Me.mimimumValue = 0
088 End Sub
089 Private maximumValue As Integer
090
091 Public Property Maximum() As Integer
092 Get
093 Return Me.maximumValue
094 End Get
095 Set(ByVal value As Integer)
096 Me.maximumValue = value
097 End Set
098 End Property
099
100
101
102 Private mimimumValue As Integer
103
104 Public Property Mimimum() As Integer
105 Get
106 Return Me.mimimumValue
107 End Get
108 Set(ByVal value As Integer)
109 Me.mimimumValue = value
110 End Set
111 End Property
112
113
114
115 Public Overrides ReadOnly Property ValueType() As Type
116
117 Get
118
119 Return GetType(Integer)
120
121 End Get
122
123 End Property
124
125
126
127 Public Overrides ReadOnly Property DefaultNewRowValue() As Object
128
129 Get
130
131 Return 0
132
133 End Get
134
135 End Property
136
137
138
139 Public Overrides Function Clone() As Object
140 Dim cell As DataGridViewProgressBarCell = CType(MyBase.Clone(),DataGridViewProgressBarCell)
141 cell.Maximum = Me.Maximum
142 cell.Mimimum = Me.Mimimum
143 Return cell
144 End Function
145
146
147
148 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)
149 Dim intValue As Integer = 0
150 If TypeOf value Is Integer Then
151 intValue = CInt(value)
152 End If
153 If intValue < Me.mimimumValue Then
154 intValue = Me.mimimumValue
155 End If
156
157 If intValue > Me.maximumValue Then
158 intValue = Me.maximumValue
159 End If
160
161 Dim rate As Double = CDbl(intValue - Me.mimimumValue) / (Me.maximumValue - Me.mimimumValue)
162 If (paintParts And DataGridViewPaintParts.Border) = DataGridViewPaintParts.Border Then
163 Me.PaintBorder(graphics,clipBounds,cellBounds,cellStyle,advancedBorderStyle)
164 End If
165 Dim borderRect As Rectangle = Me.BorderWidths(advancedBorderStyle)
166 Dim paintRect As New Rectangle(cellBounds.Left + borderRect.Left,cellBounds.Top + borderRect.Top,cellBounds.Width - borderRect.Right,cellBounds.Height - borderRect.Bottom)
167 Dim isSelected As Boolean = ((cellState And DataGridViewElementStates.Selected) = DataGridViewElementStates.Selected)
168 Dim bkColor As Color
169 If isSelected AndAlso (paintParts And DataGridViewPaintParts.SelectionBackground) = DataGridViewPaintParts.SelectionBackground Then
170 bkColor = cellStyle.SelectionBackColor
171 Else
172 bkColor = cellStyle.BackColor
173 End If
174 If (paintParts And DataGridViewPaintParts.Background) = DataGridViewPaintParts.Background Then
175 Dim backBrush As New SolidBrush(bkColor)
176 Try
177 graphics.FillRectangle(backBrush,paintRect)
178 Finally
179 backBrush.dispose()
180 End Try
181 End If
182 paintRect.Offset(cellStyle.Padding.Right,cellStyle.Padding.Top)
183 paintRect.Width -= cellStyle.Padding.Horizontal
184 paintRect.Height -= cellStyle.Padding.Vertical
185 If (paintParts And DataGridViewPaintParts.ContentForeground) = DataGridViewPaintParts.ContentForeground Then
186 If ProgressBarRenderer.IsSupported Then
187 ProgressBarRenderer.DrawHorizontalBar(graphics,paintRect)
188 Dim barBounds As New Rectangle(paintRect.Left + 3,paintRect.Top + 3,paintRect.Width - 4,paintRect.Height - 6)
189 barBounds.Width = CInt(Math.Round((barBounds.Width * rate)))
190 ProgressBarRenderer.DrawHorizontalChunks(graphics,barBounds)
191 Else
192 graphics.FillRectangle(Brushes.White,paintRect)
193 graphics.DrawRectangle(Pens.Black,paintRect)
194 Dim barBounds As New Rectangle(paintRect.Left + 1,paintRect.Top + 1,paintRect.Width - 1,paintRect.Height - 1)
195 barBounds.Width = CInt(Math.Round((barBounds.Width * rate)))
196 graphics.FillRectangle(Brushes.Blue,barBounds)
197 End If
198 End If
199 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
200 Dim focusRect As Rectangle = paintRect
201 focusRect.Inflate(-3,-3)
202 ControlPaint.DrawFocusRectangle(graphics,focusRect)
203 End If
204 If (paintParts And DataGridViewPaintParts.ContentForeground) = DataGridViewPaintParts.ContentForeground Then
205 Dim txt As String = String.Format("{0}%",Math.Round((rate * 100)))
206 Dim flags As textformatFlags = textformatFlags.HorizontalCenter Or textformatFlags.VerticalCenter
207 Dim fColor As Color = cellStyle.ForeColor
208 paintRect.Inflate(-2,-2)
209 TextRenderer.DrawText(graphics,txt,cellStyle.Font,paintRect,fColor,flags)
210
211 End If
212 If (paintParts And DataGridViewPaintParts.ErrorIcon) = DataGridViewPaintParts.ErrorIcon AndAlso Me.DataGridView.ShowCellErrors AndAlso Not String.IsNullOrEmpty(errorText) Then
213 Dim iconBounds As Rectangle = Me.GetErrorIconBounds(graphics,rowIndex)
214
215 iconBounds.Offset(cellBounds.X,cellBounds.Y)
216
217 Me.PaintErrorIcon(graphics,iconBounds,errorText)
218
219 End If
220
221 End Sub
222
223 End Class
224
225
226
227 End Class
228
229 调用
230
231 Dim pbColumn As New DataGridViewPrassBar.DataGridViewProgressBarColumn()
232 pbColumn.DataPropertyName = "Column1"
233 DataGridView1.Columns.Add(pbColumn)

[图片] 未命名.jpg

[代码] [ASP/Basic]代码

001 Imports System
002 Imports System.Drawing
003 Imports System.Windows.Forms
004 Public Class DataGridViewPrassBar
005 Public Class DataGridViewProgressBarColumn
006 Inherits DataGridViewTextBoxColumn
007
008 Public Sub New()
009 Me.CellTemplate = New DataGridViewProgressBarCell()
010 End Sub
011
012 Public Overrides Property CellTemplate() As DataGridViewCell
013 Get
014 Return MyBase.CellTemplate
015 End Get
016 Set(ByVal value As DataGridViewCell)
017 If Not TypeOf value Is DataGridViewProgressBarCell Then
018 Throw New InvalidCastException("DataGridViewProgressBarCellオブジェクトを" + "指定してください。")
019 End If
020 MyBase.CellTemplate = value
021 End Set
022
023 End Property
024
025
026
027 Public Property Maximum() As Integer
028 Get
029 Return CType(Me.CellTemplate,errorText)
218
219 End If
220
221 End Sub
222
223 End Class
224
225
226
227 End Class
228
229 调用
230
231 Dim pbColumn As New DataGridViewPrassBar.DataGridViewProgressBarColumn()
232 pbColumn.DataPropertyName = "Column1"
233 DataGridView1.Columns.Add(pbColumn)

[图片] 未命名.jpg

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...