场景是这样、我需要DataGirdView某几列数量大于0字体就变成蓝色,某几列超过标准值字体就变成红色
具体列名属性
void InitCols()
{
var col = _DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "OrderNo", " 工单号");
col.Width = 125;
// col.Frozen = true;
_DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "PrdCode", "料号");
_DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "MaterialName", "型号");
col = _DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "PlanStartDate", " 计划工单时间");
col.DefaultCellStyle.Format = "yyyy-MM-dd";
// col.Frozen = true;
col = _DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "OrderCount", " 工单投产量(K)");
col.DefaultCellStyle.Format = "0.###";
//col.Frozen = true;
col = _DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "GJKS", " 固晶");
col.DefaultCellStyle.Format = "0.###";
col = _DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "GJJS", " 待固晶");
col.DefaultCellStyle.Format = "0.###";
col = _DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "HXKS", " 焊线");
// col.Frozen = true;
col.DefaultCellStyle.Format = "0.###";
col = _DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "HXJS", " 待焊线");
// col.Frozen = true;
col.DefaultCellStyle.Format = "0.###";
col = _DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "MYKS", " 模压");
//col.Frozen = true;
col.DefaultCellStyle.Format = "0.###";
col = _DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "MYJS", " 待模压");
//col.Frozen = true;
col.DefaultCellStyle.Format = "0.###";
col = _DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "QGKS", " 切割");
//col.Frozen = true;
col.DefaultCellStyle.Format = "0.###";
col = _DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "QGJS", " 待切割");
//col.Frozen = true;
col.DefaultCellStyle.Format = "0.###";
col = _DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "FGKS", " 分光");
//col.Frozen = true;
col.DefaultCellStyle.Format = "0.###";
col = _DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "FGJS", " 待分光");
//col.Frozen = true;
col.DefaultCellStyle.Format = "0.###";
col = _DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "BDKS", " 编带");
//col.Frozen = true;
col.DefaultCellStyle.Format = "0.###";
col = _DataGridView.BuildCol<DataGridViewTextBoxColumn>(dgvDetail, "BDJS", " 编带结束");
// col.Frozen = true;
col.DefaultCellStyle.Format = "0.###";
}
添加事件
在列名字前加上DataGirdView_col就能检索到对应列
方法的具体编写
private void dgvDetail_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { DataGridView dgv = (DataGridView)sender;
// 判断当前单元格所在的列是否是待完成列
// 您可以根据列名或者列索引来判断
// 在这个示例中,我们假设这些列是按列名来判断的
string[] completionColumns = { "dgvDetail_colGJJS", "dgvDetail_colHXJS", "dgvDetail_colMYJS", "dgvDetail_colQGJS", "dgvDetail_colFGJS", "dgvDetail_colBDJS" };
string columnName = dgv.Columns[e.ColumnIndex].Name;
// 如果列名在待完成列数组中
if (completionColumns.Contains(columnName))
{
// 获取单元格的值
if (e.Value != null && decimal.TryParse(e.Value.ToString(), out decimal cellValue))
{
// 如果单元格的值大于 0,则将字体颜色设置为蓝色
if (cellValue > 0)
{
e.CellStyle.ForeColor = Color.DarkBlue;
}
else
{
// 重置为默认颜色
e.CellStyle.ForeColor = dgv.DefaultCellStyle.ForeColor;
}
}
}
string[] processColumns = { "dgvDetail_colGJKS", "dgvDetail_colHXKS", "dgvDetail_colMYKS", "dgvDetail_colQGKS", "dgvDetail_colFGKS", "dgvDetail_colBDKS" };
// 检查列名是否是工序实际数量列
if (processColumns.Contains(columnName))
{
// 获取当前行
var row = dgv.Rows[e.RowIndex];
// 获取当前单元格的实际数量值
if (decimal.TryParse(row.Cells[columnName].Value?.ToString(), out decimal actualQty))
{
// 获取工单投产量
if (decimal.TryParse(row.Cells["dgvDetail_colOrderCount"].Value?.ToString(), out decimal planQty))
{
// 比较实际数量和工单投产量
if (actualQty > planQty)
{
// 如果实际数量大于工单投产量,将单元格的字体颜色设置为红色
e.CellStyle.ForeColor = Color.Red;
}
else
{
// 否则保持默认颜色
e.CellStyle.ForeColor = dgv.DefaultCellStyle.ForeColor;
}
}
}
}
}