Eu precisava mostrar em um datagridview um progressbar e procurando pela internet achei esse código e agora disponibilizo a vocês.

Segue o código:

[sections] [section title=”Código Fonte C#”]

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Runtime.Remoting.Messaging;
using System.Windows.Forms;

public class DataGridViewProgressColumn : DataGridViewImageColumn
{
public DataGridViewProgressColumn()
{
CellTemplate = new DataGridViewProgressCell();
}

public override sealed DataGridViewCell CellTemplate
{
get { return base.CellTemplate; }
set { base.CellTemplate = value; }
}
}

internal class DataGridViewProgressCell : DataGridViewImageCell
{
// Used to make custom cell consistent with a DataGridViewImageCell
private static readonly Image EmptyImage;

static DataGridViewProgressCell()
{
EmptyImage = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
}

public DataGridViewProgressCell()
{
ValueType = typeof (int);
}

public override sealed Type ValueType
{
get { return base.ValueType; }
set { base.ValueType = value; }
}

// 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 int.
protected override object GetFormattedValue(object value,
int rowIndex, ref DataGridViewCellStyle cellStyle,
TypeConverter valueTypeConverter,
TypeConverter formattedValueTypeConverter,
DataGridViewDataErrorContexts context)
{
return EmptyImage;
}

protected override void Paint(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
DataGridViewElementStates cellState, object value, object formattedValue, string errorText,
DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
if (value==null) return;
const int borda = 2;

var progressVal = (float)value;
var percentage = (float) Math.Round((progressVal/100.0f),2);
// Need to convert to float before division; otherwise C# returns int which is 0 for anything but 100%.
//Brush backColorBrush = new SolidBrush(cellStyle.BackColor);
Brush foreColorBrush = new SolidBrush(cellStyle.ForeColor);
// Draws the cell grid
base.Paint(g, clipBounds, cellBounds,
rowIndex, cellState, value, formattedValue, errorText,
cellStyle, advancedBorderStyle, (paintParts & ~DataGridViewPaintParts.ContentForeground));
if (percentage < 0.0)
{

Color colorAtual;
Color colorAtual2;

if (progressVal < 20)
{
colorAtual = Color.DarkRed;
colorAtual2 = Color.Red;
}
else if (progressVal < 50)
{
colorAtual = Color.Goldenrod;
colorAtual2 = Color.Yellow;
}
else
{
colorAtual = Color.Green;
colorAtual2 = Color.PaleGreen;
}

Brush b = new SolidBrush(colorAtual); // Create a brush that will draw the background of the Pbar
// Calculate how much has the Pbar to be filled for "x" %
var width = (int) ((percentage*cellBounds.Width – (2*borda)));
g.FillRectangle(b, cellBounds.X + borda, cellBounds.Y + borda, width – (2*borda),
cellBounds.Height – (2*borda));
b.Dispose();

// Create a linear gradient that will be drawn over the background. FromArgb means you can use the Alpha value wich is the transparency
var lb =
new LinearGradientBrush(
new Rectangle(cellBounds.X + borda, cellBounds.Y + borda, cellBounds.Width – (2*borda),
cellBounds.Height – (2 * borda)), Color.FromArgb(255, colorAtual2),
Color.FromArgb(50, colorAtual), LinearGradientMode.BackwardDiagonal);g.FillRectangle(lb, cellBounds.X + borda, cellBounds.Y + borda, width – (2*borda),
cellBounds.Height – (2*borda));
lb.Dispose();

string strTeste = progressVal.ToString("0.00") + "%";
SizeF tamTeste2 = g.MeasureString(strTeste, cellStyle.Font);
g.DrawString(strTeste,
cellStyle.Font, foreColorBrush,
cellBounds.X + ((cellBounds.Width/2f) – (tamTeste2.Width/2f)),
cellBounds.Y + ((cellBounds.Height/2f) – (tamTeste2.Height/2f)),
StringFormat.GenericTypographic);
}
else
{
// draw the text
string strTeste = progressVal.ToString("0.00") + "%";
SizeF tamTeste2 = g.MeasureString(strTeste, cellStyle.Font);
g.DrawString(strTeste,
cellStyle.Font, DataGridView.CurrentRow != null && DataGridView.CurrentRow.Index == rowIndex
? new SolidBrush(cellStyle.SelectionForeColor)
: progressVal &amp;gt; 60 ? new SolidBrush(Color.White) : foreColorBrush,
cellBounds.X + ((cellBounds.Width/2f) – (tamTeste2.Width/2f)),
cellBounds.Y + ((cellBounds.Height/2f) – (tamTeste2.Height/2f)),
StringFormat.GenericTypographic);
}
}

Color ContrastColor(Color color)
{
int d = 0;

// Counting the perceptive luminance – human eye favors green color…
double a = 1 – (0.299 * color.R + 0.587 * color.G + 0.114 * color.B) / 255;

if (a < 0.5)
d = 0; // bright colors – black font
else
d = 255; // dark colors – white font

return Color.FromArgb(d, d, d);
}
}

Ao inserir essa classe no seu código e recompilar o código, aparecerá a opção de formatação de célula como progressbar.

[/section] [/sections]
Obrigado e até o próximo post.

DATEK Tecnologia Eletrônica
[C#] Classes para adicionar um progressbar em um datagridview
Tags:                 

Deixe uma resposta

%d blogueiros gostam disto: