我怎样才能让一个DataGridView单元格的字体特定的颜色? [英] How can I make a DataGridView cell's font a particular color?
本文介绍了我怎样才能让一个DataGridView单元格的字体特定的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此代码工作正常,为使单元格的背景蓝色:
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.
我怎样才能让字体颜色发黄?
How can I make the font color yellow?
推荐答案
您能做到这一点:
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;
更新
所以,如果你想基于具有在单元格中的值的颜色,这样的事情会做的伎俩:
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;
}
}
这篇关于我怎样才能让一个DataGridView单元格的字体特定的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文