1
votes

I want my DataGridView to highlight row headers for any rows that have selected cells. Any way to do that?

(Standard behavior seems to be only to highlight the row header if the entire row of cells is selected).

What I've tried:

I've looked at the decompiled source for DataGridView and from what i can tell (which definitely may be wrong) it highlights the header based upon whether the DataGridViewRow is selected. I can't figure out how to set the row selected without the whole row actually being selected

1
What have you tried? Have you looked at having an event handler for the CellClick or the SelectionChanged events that highlights the relevant headers?user4843530
question updated.CoderBrien

1 Answers

1
votes

The way I've done this before is to handle the CellStateChanged event, check the cells in the effected cell's row, and set the row header accordingly.

private void DataGridView1_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
    bool selected = false;

    foreach (DataGridViewCell cell in e.Cell.OwningRow.Cells)
    {
        if (cell.Selected)
        {
            selected = true;
            break;
        }
    }

    e.Cell.OwningRow.HeaderCell.Style.BackColor = selected ?
        this.dataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor :
        this.dataGridView1.RowHeadersDefaultCellStyle.BackColor;
}

You'll need to set the following snippit to enable these changes, as explained here:

this.dataGridView1.EnableHeadersVisualStyles = false;

Not shown: also consider coloring the header cell for Row[0] when everything is initialized.