Friday, May 28, 2010

Song In The Hp Commercial

Integrating a bar graph in a

may sometimes be useful to display a list of data (in a DataGridView) with a graphic display of percentage in each line of the list. For example, imagine we have a list of files and want to show graphically the rate of implementation of each of them. This is one of the best ways.

This article will explain how to get progress bars put those greenhouse "glass" and the color you want in a column in a DataGridView.

share
The code also allows signal, such as a target in order to compare the real value to this goal. See this example: it is marked with green diffuser to achieve the objective. The lines that exceed what are green and those that do not come in red.

All this is achieved using a single method that, being properly informed of what we do all the work for us.

This method must be called from the event CellPainting the DataGridView. Here's the code of the method in all its forms to be called ( polymorphism ): Public Class Bar
 

'''\u0026lt;summary>
''' Paint the background of a cell with color gradient.
'''\u0026lt;/ summary>
'''\u0026lt;param name="oColor"> Color to aplica. \u0026lt;/ Param>
''' \u0026lt;param name="e"> paramètres CellPainting the event \u0026lt;/ param>
'''\u0026lt; ; remarks> \u0026lt;/ remarks> Public Shared Sub
PintaDegradado (ByVal oColor As Drawing.Color, ByVal e As DataGridViewCellPaintingEventArgs)
Main.PintaDegradado (oColor, and, -1) End Sub


'''\u0026lt;summary>
'''Pinta de bar en progreso de un control DataGridView CELDA a
''' \u0026lt;/ summary>
'''\u0026lt;param Name="oColor"> Color to use for the bar. \u0026lt;/ Param>
''' \u0026lt;param name="e"> CellPainting event parameters. \u0026lt;/ Param>
'''\u0026lt;param name="iPorcentaje"> Rate to represent. \u0026lt;/ param>
''' \u0026lt;remarks> \u0026lt;/ remarks> Public Shared Sub
PintaDegradado (ByVal oColor As Drawing.Color, ByVal e As DataGridViewCellPaintingEventArgs, ByVal iPorcentaje As Integer) As Drawing.Color
generously padded Dim () = {}
oColor Apor Dim As Integer () = {} iPorcentaje If

iPorcentaje = -1 Then Dim aPorN As Integer () = {}
Main.PintaDegradado (acholic, e, aPorN)

Main.PintaDegradado Else (acholic, e, Apor)
End If End Sub


'
\u0026lt;summary>'''''Paint a progress bar target signaling in a cell of a DataGridView control.
'''\u0026lt;/ summary>
''' \u0026lt;param name="oColor"> Color to use for the bar. \u0026lt;/ Param>
'''\u0026lt;param name="e"> Parameters CellPainting event. \u0026lt;/ param>
'''\u0026lt;param name="iPorcentaje"> Rate to represent. \u0026lt;/ Param>
''' \u0026lt;param name="iObjetivo"> \u200b\u200bGoal to score. \u0026lt;/ Param>
''' name="oColorObjetivo"> \u200b\u200b\u0026lt;param Color to use for the purpose. \u0026lt;/ param>
'''\u0026lt;remarks> \u0026lt;/ remarks> Public Shared Sub
PintaDegradado (ByVal oColor As Drawing.Color, ByVal e As DataGridViewCellPaintingEventArgs, ByVal iPorcentaje As Integer, ByVal ïLens As Integer, ByVal oColorObjetivo As Drawing.Color)
generously padded Dim As Drawing.Color () = {OColor, oColorObjetivo}
Apor Dim As Integer () = {iPorcentaje, ïLens}
Main.PintaDegradado (acholic, e, Apor)


End Sub'''\u0026lt;summary>
''' Paint a bar Gradient background color of a cell in a grid.
'''\u0026lt;/ summary>
''' \u0026lt;param name="aColores"> array of colors to use. \u0026lt;/ Param>
'''\u0026lt;param name="e"> Parameters CellPainting event \u0026lt;/ param>
'''Matrix name="aPorcentajes"> \u0026lt;param the percentages to display. May be one or two. The first indicates
'''the percentage of the progress bar to show. The second goal to score. You indicate only one and zero,
'''will cover the entire bottom of the cell with the first color specified. \u0026lt;/ Param>
''' \u0026lt;remarks> \u0026lt;/ remarks> Private Shared Sub
PintaDegradado (ByVal aColores As Drawing.Color (), ByVal e As DataGridViewCellPaintingEventArgs, ByVal aPorcentajes As Integer ()) Dim
oPin1
As Drawing2D.LinearGradientBrush = Nothing Dim As oPin2 Drawing2D.LinearGradientBrush = Nothing Dim view As Drawing2D.LinearGradientBrush
= Nothing
Dim oColor As Drawing.Color = aColores(0)
Try
Dim oCelda As New Rectangle(e.CellBounds.X - 1, e.CellBounds.Y - 1, e.CellBounds.Width, e.CellBounds.Height)
For iC As Integer = 0 To aPorcentajes.Length - 1
If aPorcentajes(iC) > 100 Then aPorcentajes(iC) = 100
Next
Dim oRect1 As Rectangle
Dim oRect2 As Rectangle
Dim oObj As Rectangle
Dim oFond As Rectangle
Dim oCuad As Rectangle = Nothing
Dim iPorcentaje As Integer = 0
Dim bPor As Boolean = False
If aPorcentajes.Length > 0 Then
bPor = True
iPorcentaje = aPorcentajes(0)
If iPorcentaje > 0 Then
oRect1 = New Rectangle(oCelda.X + 4, oCelda.Y + 4, Math.Round(((oCelda.Width - 7) * iPorcentaje * 0.01) + 0.49), Math.Round((oCelda.Height - 8) / 2))
If oRect1.Width > oCelda.Width - 7 Then oRect1.Width = oCelda.Width - 7
oRect2 = New Rectangle(oCelda.X + 4, oRect1.Bottom - 1, oRect1.Width, (oCelda.Height - 6) - oRect1.Height)
oFond = New Rectangle(oCelda.X + 4, oCelda.Y + 4, oCelda.Width - 7, oCelda.Height - 7)
oPin1 = New Drawing2D.LinearGradientBrush(oRect1, Color.White, Color.FromArgb(180, oColor), Drawing2D.LinearGradientMode.Vertical)
oPin2 = New Drawing2D.LinearGradientBrush(oRect2, oColor, Color.FromArgb(70, oColor), Drawing2D.LinearGradientMode.Vertical)
End If
If aPorcentajes.Length > 1 Then
Dim iObj As Integer = aPorcentajes(1)
Dim iPos As Integer = oCelda.X + 4 + Math.Round(((oCelda.Width - 7) * iObj * 0.01) + 0.49)
Dim iIni As Integer = iPos - 20
If iIni < oCelda.X + 4 Then iIni = oCelda.X + 4
oObj = New Rectangle(iIni, oCelda.Y + 2, iPos - iIni, oCelda.Height - 4)
oPinO = New Drawing2D.LinearGradientBrush(oObj, Drawing.Color.FromArgb(0, aColores(1)), aColores(1), Drawing2D.LinearGradientMode.Horizontal)
End If
oCuad = New Rectangle(oCelda.X + 3, oCelda.Y + 3, oCelda.Width - 6, oCelda.Height - 6)
Else
oRect1 = New Rectangle(oCelda.X + 1, oCelda.Y + 1, oCelda.Width - 1, Math.Round(oCelda.Height / 2))
oRect2 = New Rectangle(oCelda.X + 1, oRect1.Bottom - 1, oCelda.Width - 1, oCelda.Height - oRect1.Height)
oFond = New Rectangle(oCelda.X + 1, oCelda.Y + 1, oCelda.Width - 1, oCelda.Height)
oPin1 = New Drawing2D.LinearGradientBrush(oRect1, Color.White, Color.FromArgb(180, oColor), Drawing2D.LinearGradientMode.Vertical)
oPin2 = New Drawing2D.LinearGradientBrush(oRect2, oColor, Color.FromArgb(70, oColor), Drawing2D.LinearGradientMode.Vertical)
End If
If bPor Then
e.Graphics.DrawRectangle(Pens.DimGray, oCuad)
End If
If oPin1 IsNot Nothing Then
e.Graphics.FillRectangle(Brushes.White, oFond)
e.Graphics.FillRectangle(oPin1, oRect1)
e.Graphics.FillRectangle(oPin2, oRect2)
End If
If oPinO IsNot Nothing Then
e.Graphics.FillRectangle(oPinO, oObj)
End If
e.PaintContent(oCelda)
e.Paint(oCelda, DataGridViewPaintParts.Border)
e.Handled = True
Catch ex As Exception
Debug.Print(ex.Message)
Finally
If oPin1 IsNot Nothing Then
oPin1.Dispose()
oPin1 = Nothing
End If
If oPin2 IsNot Nothing Then
oPin2.Dispose()
oPin2
= Nothing End If If
IsNot Nothing Then view
oPinO.Dispose ()
view = Nothing End If


End Try End Sub End Class

This method is ready to be called from the event CellPainting the DataGridView. Here's an example: Private Sub
 ctlLista_CellPainting (ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles ctlLista.CellPainting 

Try If e.ColumnIndex \u0026lt;0 OrElse e.RowIndex \u0026lt;0 Then Exit Sub

e.Handled = True Dim As Objetos.CapturaPesos.Registro Orowan = Nothing
Select Case DirectCast(e.ColumnIndex, Columnas)
Case Columnas.Peso
oRow = DirectCast(ctlLista.Rows(e.RowIndex).DataBoundItem, Equin.ApplicationFramework.ObjectView(Of Objetos.CapturaPesos.Registro)).Object
If oRow.Peso >= oRow.PesoMinimo AndAlso oRow.Peso <= oRow.PesoMaximo Then
Barras.PintaDegradado(Color.LightGreen, e)
Else
Barras.PintaDegradado(Color.Red, e)
End If
Case Else
e.Paint(e.CellBounds, DataGridViewPaintParts.All)


End Select Catch ex As Exception
Debug.Print (ex.Message) End Try


End Sub That's it. I hope you enjoy the trick.

0 comments:

Post a Comment