C# 如何使 DataGridView 单元格的字体具有特定颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12202751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make a DataGridView cell's font a particular color?
提问by B. Clay Shannon
This code works fine for making the cell's background Blue:
此代码适用于使单元格的背景为蓝色:
DataGridViewRow dgvr = dataGridViewLifeSchedule.Rows[rowToPopulate];
dgvr.Cells[colName].Style.BackColor = Color.Blue;
dgvr.Cells[colName].Style.ForeColor = Color.Yellow;
...but the ForeColor's effects are not what I expected/hoped: the font color is still black, not yellow.
...但 ForeColor 的效果不是我所期望/希望的:字体颜色仍然是黑色,而不是黄色。
How can I make the font color yellow?
如何使字体颜色变黄?
采纳答案by itsmatt
You can do this:
你可以这样做:
dataGridView1.SelectedCells[0].Style
= new DataGridViewCellStyle { ForeColor = Color.Yellow};
You can also set whatever style settings (font, for example) in that cell style constructor.
您还可以在该单元格样式构造函数中设置任何样式设置(例如字体)。
And if you want to set a particular column text color you could do:
如果你想设置一个特定的列文本颜色,你可以这样做:
dataGridView1.Columns[colName].DefaultCellStyle.ForeColor = Color.Yellow;
dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Blue;
updated
更新
So if you want to color based on having a value in the cell, something like this will do the trick:
因此,如果您想根据单元格中的值进行着色,则可以使用以下方法:
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;
}
}
回答by varg
To avoid performance issues (related to the amount of data in the
DataGridView), useDataGridViewDefaultCellStyleandDataGridViewCellInheritedStyle. Reference: http://msdn.microsoft.com/en-us/library/ha5xt0d9.aspxYou could use
DataGridView.CellFormattingto paint effected Cells on previous code limitations.In this case, you need to overwrite the
DataGridViewDefaultCellStyle, maybe.
为避免性能问题(与 中的数据量有关
DataGridView),请使用DataGridViewDefaultCellStyle和DataGridViewCellInheritedStyle。参考:http: //msdn.microsoft.com/en-us/library/ha5xt0d9.aspx您可以使用
DataGridView.CellFormatting以前的代码限制绘制受影响的单元格。在这种情况下,您可能需要覆盖
DataGridViewDefaultCellStyle。
//edit
In Reply to your comment on @itsmatt. If you want to populate a style to all rows/cells, you need something like this:
//编辑
回复您对@itsmatt的评论。如果要为所有行/单元格填充样式,则需要如下内容:
// 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;

