Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to show a selection border rather than back-colour in a DataGridView?

My DGV has row BackColors set to various colours based on business logic. When a user selects the row the colour changes to the selected row BackColor thus obscuring the pre-set colour. I would like to preserve the original colour when a row is selected and indicate selection with (perhaps) a bold border around the selected row. Is this possible? Is there an easy alternative I'm missing?

EDIT To make it clear, this is a WinForms app.

like image 473
Simon Avatar asked Sep 07 '25 14:09

Simon


1 Answers

Another possibility is to set the selection color to be a darker shade of the normal back color. This would be much simpler than having to reimplement the drawing of borders of selected rows.

So when you add a row with, say, a yellow background

Dim backColor as Color = Color.Yellow
row.DefaultCellStyle.BackColor = backColor
row.DefaultCellStyle.SelectionBackColor = Color.FromArgb(backColor.R * 3 / 4, backColor.G * 3 / 4, backColor.B * 3 / 4)

Multiplying each color component by the same number < 1 serves to darken the color, you could play around with different values here to see what pleases you the most aesthetically.

like image 74
Patrick McDonald Avatar answered Sep 10 '25 08:09

Patrick McDonald