首页 > 解决方案 > DatagridVIew 单个单元格格式

问题描述

试图在这个问题上找到答案,但没有发现任何对我有用的东西。我需要根据不同的条件更改 dataGridView 中单个单元格的字体颜色(背景颜色等)。Fof 示例 - 此 dataGridView 中同一行的另一个单元格中的值。我之前找到的所有解决方案都不能为我解决这个问题。

下面是我解决这个问题的建议。

标签: c#visual-studiowinformsdatagridviewcell-formatting

解决方案


这是我的解决方案:您需要创建事件 datagridView_cell_Formating:

dataGridView 单元格格式化

并添加类似这样的代码:

 private void datagridView_cell_Formating(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Target_column")
        {
            if ((e.Value != null) && (dataGridView1.Rows[e.RowIndex].Cells["Condition_cell"].Value.ToString() == "value"))
            {
                e.CellStyle.ForeColor = Color.Yellow;
            }
            else if ((e.Value != null))
            {
                e.CellStyle.ForeColor = Color.Red;
            }
        }
    }

希望这会对某人有所帮助。


推荐阅读