您的位置:首页 > 编程语言 > C#

DataGridView中显示进度条(ProgressBar)

2014-07-10 16:21 375 查看
本文转自:http://blog.csdn.net/xujuntao0000/article/details/6562980

using System;

using System.Drawing;

using System.Windows.Forms;

public class DataGridViewProgressBarColumn : DataGridViewTextBoxColumn

{

    public DataGridViewProgressBarColumn()

    {

        this.CellTemplate = new DataGridViewProgressBarCell();

    }

    public override DataGridViewCell CellTemplate

    {

        get

        {

            return base.CellTemplate;

        }

        set

        {

            if (!(value is DataGridViewProgressBarCell))

            {

                throw new InvalidCastException("DataGridViewProgressBarCellオブジェクトを" + "指定してください。");

            }

            base.CellTemplate = value;

        }

    }

    /// <summary>

    /// ProgressBarの最大値

    /// </summary>

    public int Maximum

    {

        get

        {

            return ((DataGridViewProgressBarCell)this.CellTemplate).Maximum;

        }

        set

        {

            if (this.Maximum == value)

                return;

            ((DataGridViewProgressBarCell)this.CellTemplate).Maximum =value;

            if (this.DataGridView == null)

                return;

            int rowCount = this.DataGridView.RowCount;

            for (int i = 0; i < rowCount; i++)

            {

                DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);

                ((DataGridViewProgressBarCell)r.Cells[this.Index]).Maximum = value;

            }

        }

    }

    /// <summary>

    /// ProgressBarの最小値

    /// </summary>

    public int Mimimum

    {

        get

        {

            return ((DataGridViewProgressBarCell)this.CellTemplate).Mimimum;

        }

        set

        {

            if (this.Mimimum == value)

                return;

            ((DataGridViewProgressBarCell)this.CellTemplate).Mimimum = value;

            if (this.DataGridView == null)

                return;

            int rowCount = this.DataGridView.RowCount;

            for (int i = 0; i < rowCount; i++)

            {

                DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);

                ((DataGridViewProgressBarCell)r.Cells[this.Index]).Mimimum = value;

            }

        }

    }

}

/// <summary>

/// ProgressBarをDataGridViewに表示する

/// </summary>

public class DataGridViewProgressBarCell : DataGridViewTextBoxCell

{

    public DataGridViewProgressBarCell()

    {

        this.maximumValue = 100;

        this.mimimumValue = 0;

    }

    

    private int maximumValue;

    public int Maximum

    {

        get

        {

            return this.maximumValue;

        }

        set

        {

            this.maximumValue = value;

        }

    }

    private int mimimumValue;

    public int Mimimum

    {

        get

        {

            return this.mimimumValue;

        }

        set

        {

            this.mimimumValue = value;

        }

    }

    public override Type ValueType

    {

        get

        {

            return typeof(int);

        }

    }

    public override object DefaultNewRowValue

    {

        get

        {

            return 0;

        }

    }

    public override object Clone()

    {

        DataGridViewProgressBarCell cell = (DataGridViewProgressBarCell)base.Clone();

        cell.Maximum = this.Maximum;

        cell.Mimimum = this.Mimimum;

        return cell;

    }

    protected override void Paint(Graphics graphics,

        Rectangle clipBounds, Rectangle cellBounds,

        int rowIndex, DataGridViewElementStates cellState,

        object value, object formattedValue, string errorText,

        DataGridViewCellStyle cellStyle,

        DataGridViewAdvancedBorderStyle advancedBorderStyle,

        DataGridViewPaintParts paintParts)

    {

        int intValue = 0;

        if (value is int)

            intValue = (int)value;

        if (intValue < this.mimimumValue)

            intValue = this.mimimumValue;

        if (intValue > this.maximumValue)

            intValue = this.maximumValue;

        double rate = (double)(intValue - this.mimimumValue) / (this.maximumValue - this.mimimumValue);

        if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)

        {

            this.PaintBorder(graphics, clipBounds, cellBounds,

                cellStyle, advancedBorderStyle);

        }

        Rectangle borderRect = this.BorderWidths(advancedBorderStyle);

        Rectangle paintRect = new Rectangle(

            cellBounds.Left + borderRect.Left,

            cellBounds.Top + borderRect.Top,

            cellBounds.Width - borderRect.Right,

            cellBounds.Height - borderRect.Bottom);

        bool isSelected = (cellState & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected;

        Color bkColor;

        if (isSelected && (paintParts & DataGridViewPaintParts.SelectionBackground) == DataGridViewPaintParts.SelectionBackground)

        {

            bkColor = cellStyle.SelectionBackColor;

        }

        else

        {

            bkColor = cellStyle.BackColor;

        }

        if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)

        {

            using (SolidBrush backBrush = new SolidBrush(bkColor))

            {

                graphics.FillRectangle(backBrush, paintRect);

            }

        }

        paintRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top);

        paintRect.Width -= cellStyle.Padding.Horizontal;

        paintRect.Height -= cellStyle.Padding.Vertical;

        if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)

        {

            if (ProgressBarRenderer.IsSupported)

            {

                ProgressBarRenderer.DrawHorizontalBar(graphics, paintRect);

                Rectangle barBounds = new Rectangle(

                    paintRect.Left + 3, paintRect.Top + 3,

                    paintRect.Width - 4, paintRect.Height - 6);

                barBounds.Width = (int)Math.Round(barBounds.Width * rate);

                ProgressBarRenderer.DrawHorizontalChunks(graphics, barBounds);

            }

            else

            {

                graphics.FillRectangle(Brushes.White, paintRect);

                graphics.DrawRectangle(Pens.Black, paintRect);

                Rectangle barBounds = new Rectangle(

                    paintRect.Left + 1, paintRect.Top + 1,

                    paintRect.Width - 1, paintRect.Height - 1);

                barBounds.Width = (int)Math.Round(barBounds.Width * rate);

                graphics.FillRectangle(Brushes.Blue, barBounds);

            }

        }

        if (this.DataGridView.CurrentCellAddress.X == this.ColumnIndex &&

             this.DataGridView.CurrentCellAddress.Y == this.RowIndex &&

             (paintParts & DataGridViewPaintParts.Focus) ==

                 DataGridViewPaintParts.Focus &&

             this.DataGridView.Focused)

        {

            Rectangle focusRect = paintRect;

            focusRect.Inflate(-3, -3);

            ControlPaint.DrawFocusRectangle(graphics, focusRect);

        }

        if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)

        {

            string txt = string.Format("{0}%", Math.Round(rate * 100));

            TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;

            Color fColor = cellStyle.ForeColor;

            paintRect.Inflate(-2, -2);

            TextRenderer.DrawText(graphics, txt, cellStyle.Font,

                paintRect, fColor, flags);

        }

        if ((paintParts & DataGridViewPaintParts.ErrorIcon) ==

            DataGridViewPaintParts.ErrorIcon &&

            this.DataGridView.ShowCellErrors &&

            !string.IsNullOrEmpty(errorText))

        {

            Rectangle iconBounds = this.GetErrorIconBounds(

                graphics, cellStyle, rowIndex);

            iconBounds.Offset(cellBounds.X, cellBounds.Y);

            this.PaintErrorIcon(graphics, iconBounds, cellBounds, errorText);

        }

    }

}

//用法

DataGridViewProgressBarColumn pbColumn = new DataGridViewProgressBarColumn();

pbColumn.DataPropertyName = "Column1";

DataGridView1.Columns.Add(pbColumn);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息