Search code examples
c#xmlwinformsdatagridviewdatagridviewcellstyle

how to set DataGridViewButtonCell BackColor using htmlColor?


I'm struggling since 2 days to with this problem.

I have an XML file which contains Colors as htmlColor Code, in my program I have a DataGridView which is showing me the values in hexCodes, and I can click on it and change the color value with a ColorDialog and the then set the cell BackColor to the selected Color and gives me the hexCode back as new value.

sorry I cant post a picture asI dont have 10 reputaions (I'm new)

what I want is that when I open the XML file in my programm, the cells should have the BackColor of what is wrriten inside the cell.

I tried this but doesn't work :(

private void dgvColors_CellFormatting(object sender, 
                                      DataGridViewCellFormattingEventArgs e)
{
  Theme theme = new Theme();
  foreach (KeyValuePair<string, Color> colour in theme.Colors)
         dgvColors.Columns["colKey"].DefaultCellStyle.BackColor = 
                   ColorTranslator.FromHtml(colour.Value.ToString());      
}

Solution

  • So the problem was with the ColorTranslator(),

    it works well with this version of code :

     private void dgvMenuColors_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
    { 
    foreach (DataGridViewRow row in dgvMenuColors.Rows) 
    { 
    row.DefaultCellStyle.BackColor = ColorTranslator.FromHtml(row.Cells[1].Value.ToString()); 
    } 
    } 
    

    :D