C# DataGridView列添加进度条

    xiaoxiao2022-07-03  166

    效果

    源码如下

    此代码为转载

    //定义进度条列 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);         }     } }

    效果   

    代码如下

    public class WDataGridViewColumn_Progress : DataGridViewImageColumn { public WDataGridViewColumn_Progress() { this.CellTemplate = new WDataGridViewCell_Progress(); this.HeaderText = "Operate"; } } public class WDataGridViewCell_Progress : DataGridViewImageCell { // Used to make custom cell consistent with a DataGridViewImageCell 用于使自定义单元格与DataGridViewImageCell保持一致 static Image emptyImage; static WDataGridViewCell_Progress() { emptyImage = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb); } public WDataGridViewCell_Progress() { this.ValueType = typeof(int); } // 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. 默认的图像单元格假设图像是一个值,尽管Progress单元格的值是int。 protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context) { return emptyImage; } /// <summary> /// 对单元格的重绘事件进行的方法重写。 /// </summary> 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) { float progressVal; if (value != null) progressVal = Convert.ToSingle(value); else progressVal = 10; float percentage = ((float)progressVal / 100.0f); // Need to convert to float before division; otherwise C# returns int which is 0 for anything but 100%. 除法前需要转换为浮点数;否则,c#将返回int值,该值对于除100%以外的任何值都为0。 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)); StringFormat StringFormat = new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center }; RectangleF Rectangle = new RectangleF(cellBounds.X, cellBounds.Y, cellBounds.Width, cellBounds.Height); if (percentage >= 0.0) { // Draw the progress bar and the text 绘制进度条和文本 g.FillRectangle(new SolidBrush(Color.FromArgb(255, 189, 242)), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 4)), cellBounds.Height - 4); g.DrawString(progressVal.ToString() + "%", cellStyle.Font, foreColorBrush, Rectangle, StringFormat); } else { // draw the text 绘制文本 if (this.DataGridView.CurrentRow.Index == rowIndex) g.DrawString(progressVal.ToString("#0.0") + "%", cellStyle.Font, new SolidBrush(cellStyle.SelectionForeColor), Rectangle, StringFormat); else g.DrawString(progressVal.ToString("#0.0") + "%", cellStyle.Font, foreColorBrush, Rectangle, StringFormat); } } }

     

    最新回复(0)