DataGridView进度条 DataGridViewProgressColumn

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

class DataGridViewProgressCell : DataGridViewImageCell
{
    public object progressCellLock = new object();

    public Image emptyImage;

    public DataGridViewProgressCell()
    {
        this.ValueType = typeof(double);
        emptyImage = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    }

    protected override object GetFormattedValue(object value,
    int rowIndex, ref DataGridViewCellStyle cellStyle,
    TypeConverter valueTypeConverter,
    TypeConverter formattedValueTypeConverter,
    DataGridViewDataErrorContexts context)
    {
        return emptyImage;
    }

    public new double Value
    {
        set
        {
            lock (progressCellLock)
            {
                try
                {
                    base.Value = Math.Round(value, 2);
                }
                catch { }
            }
        }
        get
        {
            return double.Parse(base.Value.ToString());
        }
    }
    private int Height = 23;
    private int Width = 210;
    private Color progressColor = System.Drawing.Color.FromArgb(((int)(((byte)(67)))), ((int)(((byte)(222)))), ((int)(((byte)(166)))));

    protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
    {
        base.Paint(g, clipBounds, cellBounds,
        rowIndex, cellState, value, formattedValue, errorText,
        cellStyle, advancedBorderStyle, (paintParts & ~DataGridViewPaintParts.ContentForeground));

        Graphics graphics = g;
        graphics.SmoothingMode = SmoothingMode.HighQuality;
        graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;

        using (Pen pen5 = new Pen(Color.White, Height - 2))
        {
            pen5.StartCap = LineCap.Round;
            pen5.EndCap = LineCap.Round;
            graphics.DrawLine(pen5, cellBounds.X + 30, cellBounds.Y + 30, cellBounds.X + Width + 30, cellBounds.Y + 30);
        }

        using (Pen pen6 = new Pen(progressColor, Height - 2))
        {
            pen6.StartCap = LineCap.Round;
            pen6.EndCap = LineCap.Round;
            int num4 = 0;
            if (Value > 0) num4 = (int)Math.Ceiling(Width * Value);
            graphics.DrawLine(pen6, cellBounds.X + 30, cellBounds.Y + 30, cellBounds.X + num4 + 30, cellBounds.Y + 30);
        }
    }
}

你可能感兴趣的:(DataGridView进度条 DataGridViewProgressColumn)