views:

774

answers:

2

With a DataGridView control on a Windows form, when you move the mouse over a row label (or column label) it's (the label cell) background changes to a shade of blue (or other colour depending on your Windows colour scheme no doubt).

I would like to produce that effect when moving the mouse over any cell in the grid - i.e. highlight the row label for the row the mouse is currently hovering over.

The logic for changing the style of the current row is simple enough using mouseover event. I can change other attributes of the row (like say the backcolor) but I would really like something more subtle than that and I think highlighting of the row label would be very effective.

Can it be done - if so how? (C# preferably)

A: 

You can hook into the DataGridView's CellMouseEnter and CellMouseLeave events and then change the backcolor accordingly. Something like this:

    private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex < 0 || e.ColumnIndex < 0) //column header / row headers
        {
            return;
        } 

        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.LightBlue;
    }

    private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex < 0 || e.ColumnIndex < 0) //column header / row headers
        {
            return;
        }

        this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.White;
    }
BFree
Wouldn't that change the current cell? I want to change the row label (the fixed column to the left) just like it changes when you move the mouse over it.
Stuart Helwig
Oh, my bad, I thought you meant you wanted that to happen for the cells themselves. Let me mess around a bit more...
BFree
+2  A: 
SwDevMan81