Add Progresscolumn in DataGridView

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Windows.Forms;
Using System.ComponentModel;
Using System.Drawing;
Using System.Drawing.Imaging;
Using System.Windows.Forms.VisualStyles;

Namespace Nereus.controls
{
public class Datagridviewprogresscolumn:datagridviewcolumn
{
Public Datagridviewprogresscolumn ()
{
Base. ValueType = typeof (float);
This. CellTemplate = new Datagridviewprogresscell ();
}

public override DataGridViewCell CellTemplate
{
Get
{
Return base. CellTemplate;
}
Set
{
Ensure the cell used for the template is a Progresscell.
if (value! = NULL &&
!value. GetType (). IsAssignableFrom (typeof (Datagridviewprogresscell)))
{
throw new InvalidCastException ("must be a Progresscell");
}
Base. CellTemplate = value;
}
}
}

public class Datagridviewprogresscell:datagridviewimagecell
{
private static Bitmap m_contentimage;
private static Bitmap emptyimage;
private static Brush ForeColor;
private bool isvisualstyled;

Static Datagridviewprogresscell ()
{
Emptyimage = new Bitmap (0x4b, 0x12, Pixelformat.format32bpppargb);
ForeColor = new SolidBrush (color.navy);
}

Public Datagridviewprogresscell ()
{
This. Value = 0f;
This. ValueType = typeof (float);
Base. Style.alignment = Datagridviewcontentalignment.middlecenter;
}

protected override Object GetFormattedValue (object value, int rowIndex, ref DataGridViewCellStyle CellStyle, TypeConverter Valuetypeconverter, TypeConverter formattedvaluetypeconverter, datagridviewdataerrorcontexts context)
{
int width = this. Owningcolumn.width;
int height = this. Owningrow.height;
this.isvisualstyled = Visualstyleinformation.isenabledbyuser & Visualstyleinformation.issupportedbyos;
M_contentimage = new Bitmap (width-4, height-4, Pixelformat.format32bpppargb);
using (Graphics m_contentgraphics = Graphics.fromimage (m_contentimage))
{
if (value = = null)
{
return emptyimage;
}

float num = (float) value;
M_contentgraphics.clear (color.transparent);
float num2 = (num > 100f)? 100f:num;
if (this.isvisualstyled)
{
Progressbarrenderer.drawhorizontalbar (M_contentgraphics, New Rectangle (2, 2, width-6, height-6));
if (num! = 0f)
{
Progressbarrenderer.drawhorizontalchunks (M_contentgraphics, New Rectangle (3, 3, ((width-8) * (int) num/100), height-8 ));
}
}
Else
{
M_contentgraphics.drawrectangle (Pens.black, 2, 2, width-6, height-6);
if (num! = 0f)
{
M_contentgraphics.fillrectangle (Brushes.blue, 3, 3, ((width-8) * (int) num/100), height-8);
}
}
RectangleF rect = new RectangleF (0, 0, width, height);
StringFormat SF = new StringFormat ();
sf. Alignment = Stringalignment.center;
sf. LineAlignment = Stringalignment.center;
Contentgraphics.drawstring (Num. ToString ("0.00") + "%", base. Datagridview.font, ForeColor, rect, SF);//display format: 0%
M_contentgraphics.drawstring (Num. ToString () + "%", base. Datagridview.font, ForeColor, rect, SF);
return m_contentimage;
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.