Ivan Ivan - 1 month ago 9x
C# Question

Datagridview set row bg color if expression is true

I have Order managment desktop application and i show all orders in dgridview.

All orders has three types:

Not Paid
In progress

So am trying in datagridview list to change bacground color to red only in all rows if order has status type
Not Paid
. Yellow on all orders where is status
In prigress

So does i can inside loop (foreach) with
to check if col value is
Not Paid
red bacground color? Or any other way to set this


Subscribe to the CellFormatting event.

private void DataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    if (e.ColumnIndex == 1) // specify the desired column number
        string value = e.Value.ToString();

        if (value == "Not Paid")
            e.CellStyle.BackColor = Color.Red;
        else if (value == "In Progress")
            e.CellStyle.BackColor = Color.Yellow;