C# 如何制作DataGridView单元格';字体是什么颜色?
此代码可以很好地将单元格背景设置为蓝色:C# 如何制作DataGridView单元格';字体是什么颜色?,c#,winforms,datagridview,fonts,datagridviewtextboxcell,C#,Winforms,Datagridview,Fonts,Datagridviewtextboxcell,此代码可以很好地将单元格背景设置为蓝色: DataGridViewRow dgvr = dataGridViewLifeSchedule.Rows[rowToPopulate]; dgvr.Cells[colName].Style.BackColor = Color.Blue; dgvr.Cells[colName].Style.ForeColor = Color.Yellow; …但是前景色的效果不是我所期望的:字体颜色仍然是黑色,而不是黄色 如何将字体颜色设置为黄色?您可以执行以下操作:
DataGridViewRow dgvr = dataGridViewLifeSchedule.Rows[rowToPopulate];
dgvr.Cells[colName].Style.BackColor = Color.Blue;
dgvr.Cells[colName].Style.ForeColor = Color.Yellow;
…但是前景色的效果不是我所期望的:字体颜色仍然是黑色,而不是黄色
如何将字体颜色设置为黄色?您可以执行以下操作:
dataGridView1.SelectedCells[0].Style
= new DataGridViewCellStyle { ForeColor = Color.Yellow};
您还可以在该单元格样式构造函数中设置任何样式设置(例如字体)
如果要设置特定的列文本颜色,可以执行以下操作:
dataGridView1.Columns[colName].DefaultCellStyle.ForeColor = Color.Yellow;
dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Blue;
已更新
因此,如果您希望基于单元格中的值来着色,类似这样的操作将实现以下目的:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null && !string.IsNullOrWhiteSpace(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()))
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = new DataGridViewCellStyle { ForeColor = Color.Orange, BackColor = Color.Blue };
}
else
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = dataGridView1.DefaultCellStyle;
}
}
DataGridView
中的数据量相关),请使用DataGridViewDefaultCellStyle
和DataGridViewCellInheritedStyle
。参考:DataGridView.CellFormatting
在以前的代码限制上绘制受影响的单元格DataGridViewDefaultCellStyle
回复您对@itsmart的评论。如果要将样式填充到所有行/单元格,则需要以下内容:
// Set the selection background color for all the cells.
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White;
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black;
// Set RowHeadersDefaultCellStyle.SelectionBackColor so that its default
// value won't override DataGridView.DefaultCellStyle.SelectionBackColor.
dataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor = Color.Empty;
// Set the background color for all rows and for alternating rows.
// The value for alternating rows overrides the value for all rows.
dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightGray;
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.DarkGray;
还可以尝试设置SelectionBackColor和SelectionForeColor。如果选中该行,则将使用这些颜色,而不是背景色/前景色。无法重现该问题。我的文本是蓝色背景上的黄色。您是否扩展了DataGridView并跳过了PaintCell?如果有可能的话。@Jay:不,我没有扩展和重写。下面的内容没有任何作用;事实上,当我选择一个单元格时,它会变成蓝色背景上的白色字体:dgvr.Cells[colName].Style.SelectionForeColor=Color.blue;我希望所有的单元格都是黄色加蓝色,不管怎样,不仅仅是选定的单元格。克莱,这就是你看到的选定颜色。如果您希望整列在蓝色上显示为黄色,请参阅我的更新。抱歉,我一开始不清楚:我不需要整列也不需要选择颜色在蓝色上显示为黄色,而只需要/所有有值的单元格。空单元格应该只有一个白色的背景色/无需特殊处理。请参阅我的更新-您可以在cellformatting处理程序中处理。我可以确认此解决方案。在我的小型测试应用程序上工作得非常完美。这(在CellFormatting()事件中)也适用于蓝色背景色,但对黄色前景色:if(e.Value!=string.Empty){e.CellStyle.backcolor=Color.blue;e.CellStyle.forecolor=Color.yellow;}