【发布时间】:2017-07-30 09:37:24
【问题描述】:
在 DataGridView 的 CellFormatting 或 CellPainting 事件处理程序中,我正在设置单元格的字体(加粗)和颜色(前部和背景)。
private void DataGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);
e.CellStyle.ForeColor = Color.White;
e.CellStyle.BackColor = Color.Black;
}
private void DataGrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);
e.CellStyle.ForeColor = Color.White;
e.CellStyle.BackColor = Color.Black;
}
这可以按预期工作,并且正确显示所需的字体和颜色。后来我试图从单元格中读取字体和颜色,但它们似乎是空的。
foreach (DataGridViewRow dgvr in dataGrid.Rows)
{
Font font = dgvr.Cells[0].Style.Font;
Color foreColor = dgvr.Cells[0].Style.ForeColor;
Color backColor = dgvr.Cells[0].Style.BackColor;
}
字体始终为空,颜色为空。
它们存储在哪里以及如何访问它们?
【问题讨论】:
-
看来
DataGridViewCellFormattingEventArgs.CellStyle只是临时用来格式化单元格的。因此,当您覆盖它们时,DataGridViewCell.Style将保持原样。我不确定是否可以轻松地从单元格本身获取CellFormatting事件中定义的颜色。也许定义DataGridViewCell.Style而不是使用CellFormatting事件。
标签: c# winforms datagridview