-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDataGridViewProgressBarCell.cs
More file actions
114 lines (68 loc) · 4.11 KB
/
DataGridViewProgressBarCell.cs
File metadata and controls
114 lines (68 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Gsemac.Drawing;
using Gsemac.Drawing.Extensions;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
// This implementation is based off of the implementation provided here:
// https://social.msdn.microsoft.com/Forums/windows/en-US/769ca9d6-1e9d-4d76-8c23-db535b2f19c2/sample-code-datagridview-progress-bar-column?forum=winformsdatacontrols
namespace Gsemac.Forms {
public class DataGridViewProgressBarCell :
DataGridViewImageCell {
// Public members
public bool ShowProgressPercentage { get; set; } = true;
public Color BackgroundColor { get; set; } = Color.FromArgb(230, 230, 230);
public Color ProgressColor { get; set; } = Color.FromArgb(6, 176, 37);
public Color TextColor { get; set; }
public DataGridViewProgressBarCell() {
ValueType = typeof(double);
}
public override object Clone() {
DataGridViewProgressBarCell clone = (DataGridViewProgressBarCell)base.Clone();
clone.ShowProgressPercentage = ShowProgressPercentage;
clone.BackgroundColor = BackgroundColor;
clone.ProgressColor = ProgressColor;
clone.TextColor = TextColor;
return clone;
}
// Protected members
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context) {
return emptyImage;
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) {
// Paint the default cell appearance.
DataGridViewCellStyle style = cellStyle;
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, style, advancedBorderStyle, paintParts);
// Paint the progress bar.
double progressValue = Convert.ToDouble(value);
double progressPercentage = progressValue / 100.0;
string progressStr = $"{progressValue:0.#}%";
Color outlineColor = ColorUtilities.Shade(BackgroundColor, 0.15f);
Color progressOutlineColor = ColorUtilities.Shade(ProgressColor, 0.15f);
// Paint the progress bar background.
Rectangle drawRect = new Rectangle(cellBounds.X + 2, cellBounds.Y + 2, cellBounds.Width - 4, cellBounds.Height - 4);
using (Brush brush = new SolidBrush(BackgroundColor))
graphics.FillRectangle(brush, drawRect);
using (Pen pen = new Pen(outlineColor))
graphics.DrawRectangle(pen, drawRect);
// Paint the progress bar foreground.
if (progressValue > 0) {
Rectangle progressRect = new Rectangle(drawRect.X, drawRect.Y, Convert.ToInt32(progressPercentage * drawRect.Width), drawRect.Height);
using (Brush brush = new SolidBrush(ProgressColor))
graphics.FillRectangle(brush, progressRect);
using (Pen pen = new Pen(progressOutlineColor))
graphics.DrawRectangle(pen, progressRect);
}
// Paint the progress bar text.
if (ShowProgressPercentage) {
bool isSelected = DataGridView.CurrentCell?.RowIndex == rowIndex;
Color textColor = TextColor == default ? (isSelected ? style.SelectionForeColor : style.ForeColor) : TextColor;
Font textFont = style.Font;
TextRenderer.DrawText(graphics, progressStr, textFont, drawRect, textColor, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
}
}
// Private members
private static readonly Bitmap emptyImage = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
}
}