这段代码演示了在DataGrIDVIEw中增加一个列显示进度条,在批量上传和下载的时候常用到。
进度条的变化如: DataGrIDvIEw.row(x).cell(x).value=50
改变他的值就是了。
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) |
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) |
以上是内存溢出为你收集整理的VB.NET Datagridview 增加列用来显示进度条全部内容,希望文章能够帮你解决VB.NET Datagridview 增加列用来显示进度条所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)