vb.net - Datagridview doesn't change colours when changing combobox values -


i have following problem datagrid.

i have datagridview several columns coloured , combobox. first column of datagridview , "signal name" has same values combobox.

my goal change color of row same signal name in combobox , leave others same initial colors.

i have tried following code, doesn't "reset" initial colors when run it. instead, desired row's color correctly changed but, when change value combobox value, doesn't reset usual colours should , end 2 rows coloured.

    public sub combobox1_selka() handles combobox1.selectedvaluechanged      dgv_sigdata.columns("target profit").defaultcellstyle.backcolor = color.fromargb(102, 255, 102)     dgv_sigdata.columns("avg loss").defaultcellstyle.backcolor = color.fromargb(247, 197, 141)     dgv_sigdata.columns("avg win").defaultcellstyle.backcolor = color.fromargb(102, 255, 102)     dgv_sigdata.columns("stop loss").defaultcellstyle.backcolor = color.fromargb(247, 197, 141)     dgv_sigdata.columns("target profit").defaultcellstyle.backcolor = color.beige      each row datagridviewrow in dgv_sigdata.rows         if row.cells.item(0).value.tostring = combobox1.text               dgv_sigdata.rows(row.index).cells("signal name").style.backcolor = color.fromargb(10, 2, 102)             dgv_sigdata.rows(row.index).defaultcellstyle.backcolor = color.fromargb(10, 2, 102)         else             dgv_sigdata.rows(row.index).cells("signal name").style.backcolor = color.white         end if     next row    end sub 

this code suppose reset columns initial colors , color combobox row only. yet, doesn't.

thank !

first, follow suggestions made larstech.

next, note logic of color changes:

  1. you set default colors each column. works.
  2. if first cell value of row matches combobox selection:
    1. color cells("signal name") dark blue. (redundant next line).
    2. set default color of row dark blue. (overrides column colors).
  3. else:
    1. color cells("signal name") white. works.

once row's defaultcellstyle.backcolor has been set, takes precedence on column default. change white, still overrides column colors. hierarchy seems work follows:

cell.style.backcolor -overrides-> row.defaulcellstyle.backcolor -overrides-> column.defaultcellstyle.backcolor 

solution:

public sub combobox1_selka() handles combobox1.selectedvaluechanged      each row datagridviewrow in dgv_sigdata.rows         if row.cells.item(0).value.tostring = combobox1.selecteditem.tostring             each cell datagridviewcell in row                 cell.style.backcolor = color.fromargb(10, 2, 102)             next cell         else             row.cells("target profit").style.backcolor = color.fromargb(102, 255, 102)             row.cells("avg loss").style.backcolor = color.fromargb(247, 197, 141)             row.cells("avg win").style.backcolor = color.fromargb(102, 255, 102)             row.cells("stop loss").style.backcolor = color.fromargb(247, 197, 141)             row.cells("target profit").style.backcolor = color.beige             row.cells("signal name").style.backcolor = color.white         end if     next row  end sub 

side note: you're setting columns("target profit") twice separate colors. didn't know if simple oops or copy-paste error.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -