C# 根据列名(datagrid)更改列中行中文本的所有颜色

C# 根据列名(datagrid)更改列中行中文本的所有颜色,c#,winforms,datagridview,C#,Winforms,Datagridview,我想根据C#中的列名更改列中行中文本的所有颜色。我将如何做到这一点 到目前为止,我已经尝试了以下方法: private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (this.dataGridView1.Columns[e.ColumnIndex].Name == "DatePaid") {

我想根据C#中的列名更改列中行中文本的所有颜色。我将如何做到这一点

到目前为止,我已经尝试了以下方法:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (this.dataGridView1.Columns[e.ColumnIndex].Name == "DatePaid")
            {
                e.CellStyle.ForeColor = Color.Blue;
            }
        }

程序生成-但根本不起作用

您可以使用
开关

        switch (dataGridView1.Columns[e.ColumnIndex].Name)
        {
            case "DatePaid":
                dataGridView1.Columns[e.ColumnIndex].DefaultCellStyle.ForeColor = Color.Blue;
                break;
            case "Something":
                dataGridView1.Columns[e.ColumnIndex].DefaultCellStyle.ForeColor = Color.Red;
                break;
        }

如果您想更改特定的柱前景色,请使用此

dataGridView1.Columns["DatePaid"].DefaultCellStyle.ForeColor = Color.Blue;

无法从.mobile对我的回答发表评论。您必须将该代码放入表单加载中