【问题标题】:How can I make a DataGridView cell's font a particular color?如何使 DataGridView 单元格的字体具有特定颜色?
【发布时间】:2012-08-25 12:40:38
【问题描述】:

此代码可以很好地使单元格的背景变为蓝色:

DataGridViewRow dgvr = dataGridViewLifeSchedule.Rows[rowToPopulate];
dgvr.Cells[colName].Style.BackColor = Color.Blue;
dgvr.Cells[colName].Style.ForeColor = Color.Yellow;

...但是 ForeColor 的效果不是我所期望/希望的:字体颜色仍然是黑色,而不是黄色。

如何使字体颜色变黄?

【问题讨论】:

  • 尝试同时设置 SelectionBackColor 和 SelectionForeColor。如果选择了行,将使用这些颜色,而不是 BackColor/ForeColor 的颜色。
  • 无法重现问题。我的文字是蓝色背景上的黄色。
  • 有没有机会扩展 DataGridView 并覆盖 OnPaintCell?如果是这样this 可能会有所帮助。
  • @Jay:不,我没有扩展和覆盖。

标签: c# winforms datagridview fonts datagridviewtextboxcell


【解决方案1】:

你可以这样做:

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;
    }
}

【讨论】:

  • 下面什么都不做;事实上,当我选择一个单元格时,它会变成蓝色黑色背景上的白色字体: dgvr.Cells[colName].Style.SelectionForeColor = Color.Blue;而且我希望所有单元格都是蓝色的黄色,无论如何,不​​仅仅是选定的[s]。
  • 这是您看到的选择颜色,Clay。如果您希望整个列在蓝色上显示为黄色,请参阅我的更新。
  • 抱歉,起初我并不清楚:我既不需要整个列,也不需要选择颜色为蓝色上的黄色,而只需要/所有具有值的单元格。空单元格应该只有白色背景/没有特殊处理。
  • 查看我的更新 - 您可以在单元格格式化处理程序中处理它。
  • 我可以确认这个解决方案。在我的小型测试应用程序上完美运行。
【解决方案2】:
  1. 为避免性能问题(与DataGridView 中的数据量有关),请使用DataGridViewDefaultCellStyleDataGridViewCellInheritedStyle。参考:http://msdn.microsoft.com/en-us/library/ha5xt0d9.aspx

  2. 您可以使用DataGridView.CellFormatting 在之前的代码限制下绘制受影响的单元格。

  3. 在这种情况下,您可能需要覆盖DataGridViewDefaultCellStyle

//编辑
回复您对@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;

【讨论】:

  • 这也(在 CellFormatting() 事件中)适用于蓝色背景色,但没有任何作用:黄色前景色: if (e.Value != string.Empty) { e.CellStyle.BackColor = 颜色.蓝色; e.CellStyle.ForeColor = 颜色.黄色; }
猜你喜欢
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-29
  • 2014-02-17
相关资源
最近更新 更多