Hyounmin Wang
Hyounmin Wang

Reputation: 111

Binding datagridview font color in datatable c#

So right now I have set up datatable and binded it to datagridview.

What my program does is that it saves realtime log to the datatable. However I need it to have a font color in each row based on the log level so I save its font color at each row in data table (6th array of datatable)

Is there a solution to bind style to a with datatable and datagridview?

I am also using filter to search as well so I need to be able to backup to the color that it once had before the color was implented.

currently this is what i am doing. Biggest problem to this is that after filter has been implemented, the style property is all gone and it becomes again no colored colum (all black again)

if (value.Contains("<Notice>:"))
                {
                    dataGridView1.Rows[rowNumber].DefaultCellStyle.ForeColor = Color.ForestGreen;
                    logParserView.Rows[rowNumber][6] = "ForestGreen";
                }
                else if (value.Contains("<Debug>:"))
                {
                   dataGridView1.Rows[rowNumber].DefaultCellStyle.ForeColor = Color.Orange;
                    logParserView.Rows[rowNumber][6] = "Orange";
                }
                else if (value.Contains("<Info>:"))
                {
                    dataGridView1.Rows[rowNumber].DefaultCellStyle.ForeColor = Color.Orange;
                    logParserView.Rows[rowNumber][6] = "Orange";
                }
                else if (value.Contains("<Warning>:"))
                {
                    dataGridView1.Rows[rowNumber].DefaultCellStyle.ForeColor = Color.Orange;
                    logParserView.Rows[rowNumber][6] = "Orange";
                }
                else if (value.Contains("<Error>:"))
                {
                    dataGridView1.Rows[rowNumber].DefaultCellStyle.ForeColor = Color.DarkRed;
                    logParserView.Rows[rowNumber][6] = "DarkRed";
                }
                else if (value.Contains("<Critical>:"))
                {
                    dataGridView1.Rows[rowNumber].DefaultCellStyle.ForeColor = Color.DarkRed;
                    logParserView.Rows[rowNumber][6] = "DarkRed";
                }
                else if (value.Contains("<Alert>:"))
                {
                    dataGridView1.Rows[rowNumber].DefaultCellStyle.ForeColor = Color.DarkRed;
                    logParserView.Rows[rowNumber][6] = "DarkRed";
                }
                else if (value.Contains("<Emergency>:"))
                {
                    dataGridView1.Rows[rowNumber].DefaultCellStyle.ForeColor = Color.IndianRed;
                    logParserView.Rows[rowNumber][6] = "IndianRed";
                }

Thanks

Upvotes: 1

Views: 1031

Answers (1)

Luc Morin
Luc Morin

Reputation: 5380

If I understand correctly, you simply need to handle the DataGridView.CellFormatting event, and in the handler, set the CellStyle of the DataGridViewCellFormattingEventArgs handler argument.

Cheers

EDIT:

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.Value == someOtherValue)
            e.CellStyle.BackColor = Color.Red;
    }

Upvotes: 1

Related Questions