DataGridView Progressbar help

compguru910

Well-known member
Joined
Nov 25, 2009
Messages
47
Programming Experience
3-5
Ok, this is an oddball one. I found this class to make a column in the datagridview a progress bar. I got that working fine. The problem im running into is I can figure out how to set the progress bar values. I create the column, and it gives each row a progress bar, but no values. How would I programatically go through each row and set the values? I have no experience with the combobox or image box columns either, datagridview is a wierd control.
Here is the class for the progresscolumn
VB.NET:
Imports System.ComponentModel
Public Class DataGridViewProgressColumn
    Inherits DataGridViewImageColumn
    Public Sub New()
        Me.CellTemplate = New DataGridViewProgressCell
    End Sub
End Class
Public Class DataGridViewProgressCell
    Inherits DataGridViewImageCell

    Sub New()
        ValueType = Type.GetType("Integer")
    End Sub
    ' Method required to make the Progress Cell consistent with the default Image Cell.
    ' The default Image Cell assumes an Image as a value, although the value of the Progress Cell is an Integer.
    Protected Overrides Function GetFormattedValue( _
        ByVal value As Object, _
        ByVal rowIndex As Integer, _
        ByRef cellStyle As DataGridViewCellStyle, _
        ByVal valueTypeConverter As TypeConverter, _
        ByVal formattedValueTypeConverter As TypeConverter, _
        ByVal context As DataGridViewDataErrorContexts _
        ) As Object
        Static emptyImage As Bitmap = New Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        GetFormattedValue = emptyImage
    End Function
    Protected Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal cellBounds As System.Drawing.Rectangle, _
ByVal rowIndex As Integer, ByVal cellState As System.Windows.Forms.DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, _
ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle, _
ByVal paintParts As System.Windows.Forms.DataGridViewPaintParts)
        Dim progressVal As Integer = CType(value, Integer)
        Dim percentage As Single = CType((progressVal / 100), Single)
        Dim backBrush As Brush = New SolidBrush(cellStyle.BackColor)
        Dim foreBrush As Brush = New SolidBrush(cellStyle.ForeColor)
        ' Call the base class method to paint the default cell appearance.
        MyBase.Paint(g, clipBounds, cellBounds, rowIndex, cellState, _
            value, FormattedValue, ErrorText, cellStyle, _
            advancedBorderStyle, paintParts)
        If percentage > 0.0 Then
            ' Draw the progress bar and the text
            g.FillRectangle(New SolidBrush(Color.FromArgb(163, 189, 242)), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 4)), cellBounds.Height - 4)
            g.DrawString(progressVal.ToString() & "%", cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
        Else
            'draw the text
            If Not Me.DataGridView.CurrentCell Is Nothing AndAlso Me.DataGridView.CurrentCell.RowIndex = rowIndex Then
                g.DrawString(progressVal.ToString() & "%", cellStyle.Font, New SolidBrush(cellStyle.SelectionForeColor), cellBounds.X + 6, cellBounds.Y + 2)
            Else
                g.DrawString(progressVal.ToString() & "%", cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
            End If
        End If
    End Sub
End Class
 
VB.NET:
Me.DataGridView1.Item(1, 1).Value = 100
'or
Me.DataGridView1.Rows(1).Cells(1).Value = 100
 
Here is my code, it produces and error (hence the On Error Resume Next)

VB.NET:
    Public Sub ColorRows()

        For Each row As DataGridViewRow In dgvMain.Rows
            On Error Resume Next
            If row.Cells(7).Value > Now Then
                row.DefaultCellStyle.BackColor = Color.Aqua
                row.Cells(15).Value = 90
            End If

            If row.Cells(7).Value < Now Then
                row.DefaultCellStyle.BackColor = Color.Red
            End If
        Next
    End Sub

This code just makes the column not visible, which I believe is an error in that column
 
Back
Top