Search code examples
vb.netdatagridviewselectionhighlighting

Multiple SelectionBackColors for a Datagridview or Disabling the Color


I'm trying to change the dgv selectionbackcolor depending on the selected cell's backcolor, so if the cell's backcolor is light red, the selectionbackcolor of that cell will be dark red.

I have a solution for single cell selection, but my problem is I have several different cell colors and when highlighting multiple cells, my selectionbackcolor is the one color across all cells. Now I don't imagion you will be able to change the selectionbackcolor cell by cell in one selection, but does anybody know away of disabling the selectionbackcolor, but still having the cells selected (I will color the cell by other means if I'm just able to disable the cell selection highlight). For another part of my code, I still need the cells selected (but not colored), as it uses for each selectedcell....

I've tried setting my selectionbackcolor as transparent, but that seemed to have several bugs or not preform that well and I would prefer not to use that.

I've also tryied dgv.clearselection(), but this remove the selection color and the actual selection, whereas noted above, I still need the selection.

I do have an idea of how to get around this, but my idea involve excessive amount of code, which seems unnecessary for something that seem quite simple.

Any help would be much appreciated.


Solution

  • Oh, maybe you can change each selection color as per the cell.

    Code below (provided a simple version):

            For Each DataGridViewCell In Me.dgv.SelectedCells
    
            DataGridViewCell.style.selectionbackcolor = DataGridViewCell.style.backcolor
    
        Next