2

I am importing data from an XML document to populate a DataGridView. During the import, I change the back color of some of the cells. However, as I add the rows, the color of the cells do not updated properly (I get a grey cell). I am not sure if there is a place I should be invalidating the DataGridView to get the cells to show up properly.

I should mention that my DataGridView is not data bound.

Some code for reference:

DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();

for(int j = 0; j < dataGridView1.ColumnCount; ++j)
{
   if(j == 2)
   {
      row.Cells[j + 1].Style.BackColor = layer.Color;
   }
}
this.dataGridView1.Rows.AddRange(row);
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user2970916
  • 1,146
  • 3
  • 15
  • 37
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Oct 16 '14 at 20:29
  • What was my original title? I do not remember. I would like to know what part of it was wrong. Thanks. – user2970916 Oct 16 '14 at 20:35
  • It was "C# DataGridView Importing Data". Did you mean the "C#" and "DataGridView" to categorize your question as being about C# and about the DataGridView? Or did you mean that you're using the "C# DataGridView" control? – John Saunders Oct 16 '14 at 20:39
  • I meant I was using the C# DataGridView control. Poor word ordering on my part. Thanks for letting me know. – user2970916 Oct 16 '14 at 20:40
  • Well, it's actually worse than poor word order. There _is_ no C# DataGridView Control. There's a .NET DataGridView control, but it has nothing to do with the C# programming language. The same control can be used just as well with VB.NET. – John Saunders Oct 16 '14 at 20:41

1 Answers1

1

You might want to override the CellFormatting or RowPostPaint event and do it there.

I think I ran into the same thing when trying to color my DataGridView and this is how I solved it.

private void gridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    foreach (DataGridViewRow row in gridView.Rows)
        if (row.Cells["Status"].Value.ToString() == "Posted")
            if (row.Cells["Priority"].Value.ToString() == "High")
                foreach (DataGridViewCell cell in row.Cells)
                    cell.Style.BackColor = Color.LightPink;
            else
                foreach (DataGridViewCell cell in row.Cells)
                    cell.Style.BackColor = Color.Yellow;
}

CellFormatting:

private void gridItems_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.CellStyle.BackColor != Color.Yellow && e.CellStyle.BackColor != Color.LightPink)
        e.CellStyle.BackColor = Color.LightGreen;
}

i hope this helps!

user1274820
  • 7,786
  • 3
  • 37
  • 74
  • For some reason, when I try to get the color via Color.FromArgb() it does not work. If I get a color from a built in color (Color.Red) it works. – user2970916 Oct 16 '14 at 20:19
  • "You cannot use Color.FromArgb, because DataGridView won't accept transparent colors. This is probably caused by the fact that the cells and DataGridView are not transparent (by default)." From: http://stackoverflow.com/questions/17131996/cannot-use-color-fromargb-in-datagridview-cell-backcolor Solution: http://stackoverflow.com/questions/13252838/get-colors-between-two-color-hex-refs-or-rgb – user1274820 Oct 16 '14 at 20:20
  • Yeah. I got it to work by putting an Alpha value of 255. Not sure why that works. I thought a 255 Alpha would make it completely transparent. I might be mistaken though. – user2970916 Oct 16 '14 at 20:34
  • Ahh nice, I haven't tried it myself - just looked up the question :) I will remember that for future reference – user1274820 Oct 16 '14 at 20:36