views:

536

answers:

1

The grid display all information correctly, In the event dataGridView1cellFormatting I change the backcolor depending of the object under the line value. This works too. The last event that I have on my grid is the dataGridView1_CellPainting that check if it's the header to add an icon.

Everything is fine until I try to take out the color of the selected line (or cell it does the same thing). What I would like is take out the color of the selected line. I have try to set it with "Transparent" but when the control bind data, the line is gray and when we resize the column size the text is not readable.

How can I display the data inside the DataGridView without having the selected line highlighted?

+3  A: 

You can set the SelectionForeColor and SelectionBackColor property to what ever color you want to change the hightligh color. This can be set on either the DefaultCellStyle property on the DataGridView, or on the individual cells itself. This way the colors will not change when the row is selected.

Private Sub dgv_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgv.CellFormatting
    If e.RowIndex < 0 Then Exit Sub

    If e.RowIndex Mod 2 = 0 Then
        e.CellStyle.BackColor = Color.Orange
    Else
        e.CellStyle.BackColor = Color.Red
    End If

    'Make the selected cell the same color
    e.CellStyle.SelectionBackColor = e.CellStyle.BackColor
    e.CellStyle.SelectionForeColor = e.CellStyle.ForeColor
End Sub
Ross Goddard