methuselah
methuselah

Reputation: 13206

Change all the colours of text in rows in a column depending on the column name (datagrid)

I want to change all the colours of text in rows in a column depending on the column name in C#. How would I accomplish this?

So far I've tried the following:

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

The program builds - but it doesn't work at all

Upvotes: 1

Views: 71

Answers (2)

Patrick
Patrick

Reputation: 681

You could use a switch:

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

Upvotes: 1

Kurubaran
Kurubaran

Reputation: 8902

If you want to change particular colum forecolor then use this,

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

Upvotes: 2

Related Questions