C# datagridview 单元格显示进度,数字有重复。

我贴一下代码:
1.绑定datasource
private void InitializeDataGridView()
{
// 创建 DataTable 用于存储数据
#region
dataTable = new DataTable();
dataTable.Columns.Add("Item", typeof(string));
dataTable.Columns.Add("WO", typeof(string));
dataTable.Columns.Add("PN", typeof(string));
dataTable.Columns.Add("WO Qty", typeof(int));
dataTable.Columns.Add("Testing", typeof(string));
dataTable.Columns.Add("Inspection", typeof(string));
dataTable.Columns.Add("FQC", typeof(string));
dataTable.Columns.Add("Packaging", typeof(string));
// 添加数据行
AddDataRow("931300123456", "117G0-037781-R1", 150, 150, 140, 140, 120);
AddDataRow("931303123456", "117G0-037784-R1", 30, 30, 25, 20, 0);
AddDataRow("931304123456", "117G0-037785-R1", 100, 80, 70, 70, 20);
AddDataRow("931306123456", "117G0-037787-R1", 0, 0, 0, 0, 0);
AddDataRow("931307123456", "117G0-037788-R1", 60, 60, 50, 50, 20);
#endregion
// 设置 DataGridView 的数据源
//dataGridView2.DataSource = dataTable;
#region
dataGridView2.Columns.Add("Col1","项次1");
dataGridView2.Columns.Add("Col2", "项次2");
dataGridView2.Columns.Add("Col3", "项次3");
dataGridView2.Columns.Add("Col4", "项次4");
dataGridView2.Columns.Add("Col5", "项次5");
dataGridView2.Columns.Add("Col6", "项次6");
dataGridView2.Columns.Add("Col7", "项次7");
dataGridView2.Columns.Add(new ProgressColumn()); // 添加进度列
dataGridView2.Columns.Add(new ProgressColumn());
dataGridView2.Rows.Add("931300123001", "117G0-037781-R1", "150", "150/150", "150/140","150/50","150/0","150/30","150/150");
dataGridView2.Rows.Add("931300123002", "117G0-037781-R1", "100", "100/100", "100/80", "100/50", "100/0","100/60", "60/60");
#endregion
// 设置样式
dataGridView2.CellPainting += DataGridView1_CellPainting;
}
2.添加row
private void AddDataRow(string wo, string pn, int woQty, int testing, int inspection, int fqc, int packaging)
{
dataTable.Rows.Add(new object[]
{
"项次",
wo,
pn,
woQty,
$"{woQty}/{testing}",
$"{woQty}/{inspection}",
$"{woQty}/{fqc}",
$"{woQty}/{packaging}"
});
}
3.cell_paint
private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex > 3)
{
string value = Convert.ToString(dataGridView2[e.ColumnIndex, e.RowIndex].Value);
if (string.IsNullOrEmpty(value))
return;
string[] parts = value.Split('/');
int total = int.Parse(parts[0]);
int current = int.Parse(parts[1]);
SolidBrush drawBrush = new SolidBrush(Color.Black);
if (current == total)
{
drawBrush = new SolidBrush(Color.Green);
}
else if (current > 0 && current < total)
{
drawBrush = new SolidBrush(Color.FromArgb(255, 255, 153)); // 黄色前景
dataGridView2[e.ColumnIndex, e.RowIndex].Style.SelectionBackColor = Color.Transparent; // 透明背景
}
else
{
dataGridView2[e.ColumnIndex, e.RowIndex].Style.SelectionBackColor = Color.Transparent; // 透明背景
}
e.Paint(e.CellBounds, DataGridViewPaintParts.All & DataGridViewPaintParts.ContentBackground & DataGridViewPaintParts.ContentForeground);
// e.Graphics.DrawString(value, dataGridView2.Font, drawBrush, e.CellBounds.X+2, e.CellBounds.Y+2);
drawBrush.Dispose();
}
}
4.对象
public class ProgressColumn : DataGridViewColumn
{
public ProgressColumn() : base(new ProgressCell())
{
this.CellTemplate = new ProgressCell();
}
}
5.单元格显示
public class ProgressCell : DataGridViewTextBoxCell
{
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)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
if (formattedValue != null && formattedValue.ToString().Contains("/"))
{
string[] parts = formattedValue.ToString().Split('/');
if (parts.Length == 2 && int.TryParse(parts[0], out int total) && int.TryParse(parts[1], out int completed))
{
float progress = (float)completed / total;
Brush brush;
if (progress == 1)
brush = new SolidBrush(Color.Green); // 全部完成,绿色
else if (progress > 0 && progress < 1)
brush = new SolidBrush(Color.Yellow); // 部分完成,前景黄色
else // progress == 0的情况
return; // 或者您可以根据需求添加特定处理
graphics.FillRectangle(brush, cellBounds.Left, cellBounds.Top, (int)(cellBounds.Width * progress), cellBounds.Height);
//显示单元格的值
string text = string.Format("{0}", value);
SizeF rf = graphics.MeasureString(text, cellStyle.Font);
float x = cellBounds.X + (cellBounds.Width - rf.Width) / 2f;
float y = cellBounds.Y + (cellBounds.Height - rf.Height) / 2f;
graphics.DrawString(text, cellStyle.Font, new SolidBrush(cellStyle.ForeColor), x, y);
}
}
}
}