Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#/.NET how to highlight certain rows in DataGridView

I have a DataGridView that is filled by setting its DataSource to a DataBinding.

Now I want to have certain rows in the DataGridView having different Backgroundcolor according to some value in the row itself.

How can I possibly accomplish this?

like image 303
clamp Avatar asked Aug 31 '09 12:08

clamp


3 Answers

There's a great example here.

The concept is that you subscribe to events from the grid. When a cell is filled, an event is fired and based upon the value you can format the cell etc.

like image 191
Paul Sasik Avatar answered Sep 20 '22 23:09

Paul Sasik


You can use the RowPrePaint to change the color or style of the whole row

like image 27
Thomas Levesque Avatar answered Sep 17 '22 23:09

Thomas Levesque


In the CellFormatting event handler of your datagridview you can set the default backcolor for any row you want.

private void MyDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {

        if (e.RowIndex == rowIndexToHighlight)
        {
            e.CellStyle.BackColor = Color.Green;
        }

    }
like image 38
Mez Avatar answered Sep 18 '22 23:09

Mez