GridView Row Selection : Can I use border instead of backcolor?

22-degrees

Well-known member
Joined
Feb 11, 2012
Messages
156
Location
South East Ireland
Programming Experience
1-3
I use a color scale to set the background color of cells in my dataGrid based on the cell value.

However when i select a row for more focused viewing, the default action is for the entire row to be highlighted in a solid color and this overrides my cell color feature making it useless to me until i de-select the row.

Is there a way to highlight the selected row with a border and leave the original cell backcolors intact?
 
First things first, exactly what grid are you talking about? Please use the proper names for things rather than approximations. Your title says GridView but there is no such control in standard WinForms. Your post says DataGrid but that has been obsolete since VB 2005. Do you maybe mean a DataGridView, which is the default grid for WinForms these days?
 
The last version of VS that included the DataGrid in the Toolbox by default was VS.NET 2003. The DataGridView is superior in almost every respect, hence it has been in the Toolbox by default since it's introduction in .NET 2.0 and VS 2005.

If you don't want cells painted in the default manner then you will have to handle the CellPainting event of the grid. I've never actually done it myself so I'm not sure of all the details but I would imagine that you would use the e.State property in the event handler to prevent the cell state being painted for selected cells. You should do a bit of reading on that event and the associated types and members and then do a bit of experimentation.
 
It's pretty basic, just handle the RowPrePaint event and set the selection color of the row (if it is selected) to a darker shade of the background color. It looks like a semi-transparent selection. It's not a border, but it solves your problem. It works best with lighter background colors (pastel). For darker background colors you would instead make the selection a brighter shade.

row.DefaultCellStyle.SelectionBackColor = Color.FromArgb(bgColor.R * 5 / 6, bgColor.G * 5 / 6, bgColor.B * 5 / 6)
 
The color scale at the moment ranges from red through orange and yellow to green.

I will try it out.. The color system is very sensitive but I guess if I am entering into a focus task of only viewing one row of data then a very slight color change won't interfere with how I process what I am looking at.
 
Just adjust the 5/6 fraction (about 15% darker than original) to a smaller one to have better contrast. 5/7 is about 30%, I wouldn't go much lower than this. You could also set the alpha of the other background colors to 70-85%, and set the selection as opaque, but it might blur text a bit.
 
Back
Top