سؤال

I have a DataGridView with four columns. The first three are returned back as an array: red, green and blue. All values are between 0 and 255. I've programmatically added a column called "Colour". I'd like the backcolor of this column to reflect the RGB content. I can see the rgb data correctly, and using the debugger, the backcolor is being set correctly against the property. I can also set the column's backcolor and it displays correctly. I've tried setting the "Red" cell's backcolor and this doesn't work either.

The below code results in a completely white column.

Here's my code:

        DataGridViewColumn DGC = new DataGridViewColumn();
        DGC.Name = "Colour";
        DGC.CellTemplate = new DataGridViewTextBoxCell();
        dgPallette.Columns.Add(DGC);
        foreach (DataGridViewRow DGR in dgPallette.Rows)
        {
            Color cellColor = Color.FromArgb(
                int.Parse(DGR.Cells["Red"].Value.ToString()), 
                int.Parse(DGR.Cells["Green"].Value.ToString()), 
                int.Parse(DGR.Cells["Blue"].Value.ToString()));
            DGR.Cells["Colour"].Style.BackColor = cellColor;
        }

Any ideas?

Thanks in advance Jim

هل كانت مفيدة؟

المحلول

Sometimes I find that setting colours in the DataGridView doesn't work as expected and end up resorting to using the DataGridView.CellFormatting event - this always does the trick, and is fairly straightforward to get your head around.

Often formatting problems arise when the grid is bound to a datasource - especially when you allow sorting. This event overcomes that.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top