Click here to Skip to main content
15,795,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

So I have a datagridview, it has cellformatting and depending on whether there is a value in the bindingsource, will put an image in the cell.
All good so far.
Now, the background selection colour (color for the Americans) has been changed so it uses a white or black text depending on the brightness of the custom colour that the user can assign to the DGV selected rows property value. For eg.

 DGV.DefaultCellStyle.SelectionBackColor = Color.DarkGray
DGV.DefaultCellStyle.SelectionForeColor = Color.DarkGray

So for a dark blue DGV (only on the column headings and selected rows), the font pen is changed to White and if the DGV headings and selected rows is a Yellow, then the system has been set to use Black as as the font pen.

The issue is, the graphic is a black tick in a circle box and if the DGV has been set to use, say, a dark blue, the text is white but the graphic is Black - and can be difficult to see.

Rows can be selected/unselected by the user at will.

My cellformatting just checks to see if the bindingsource value is a 1 and then assigns the circle with a tick within, normally, unselected rows, colours are white (and must be)

I have a white version of the tick within a circle, but I cannot seem to assign the graphic to those rows that are either pre-selected or are selected during the cellformatting.

What I am looking for is to be able to detect (if possible within cellformatting) if a row that cellformatting is currently looking at (its index i.e. e.rowindex), has been selected so I can use the alternative graphic that is white-outlined, instead of the black outlined so its visibly clear.

Row selections are performed either on-the-fly or are selected during the load up of the form.

What I have tried:

I have tried (in the cellformatting) to see if the e.rowindex=DGV.selectedrows (but at that point I dont know what the index of the selectedrows would be.. or e.rowindex=DGV.currentrow - which fails if nothing has yet been selected.

Updated 3-Aug-23 1:51am

1 solution

Found it. What an amateur mistake. In the cellformatting, I can just check if the e.rowindex is selected. Doh !

If DataGridView2.Rows(e.RowIndex).Selected = True Then
                           If Int32.Parse(e.Value.ToString) = 1 Then
                               e.Value = My.Resources.WhiteTick
                               e.Value = Nothing
                           End If
                           If Int32.Parse(e.Value.ToString) = 1 Then
                               e.Value = My.Resources.BlackTick
                               e.Value = Nothing
                           End If
                       End If
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900