Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change datagridview cell color when using a datasource

I've got an interesting issue. I am trying to use a datatable as a data source for a datagridview. I want to color some of the cells of the table to indicate various things, but for some reason the color will not display. So the following code shows an uncolored cell.

dataGridView1.DataSource = table;

dataGridView1.Rows[0].Cells[0].Style.BackColor = Color.Yellow;

I can only get a color to display after the initial form load (for example setting a cell color on the OnClick event). However, if I explicitly create the rows and columns for the view as in the code below, the coloring works.

foreach (DataColumn col in table.Columns)
    dataGridView1.Columns.Add(col.ColumnName, col.ColumnName);

for (int i = 0; i < table.Rows.Count; i++)
{
    var row = table.Rows[i];
    object[] values = new object[table.Columns.Count];
    for (int x = 0; x < table.Columns.Count; x++)
        values[x] = row[x].ToString();

    dataGridView1.Rows.Add(values);
}

dataGridView1.Rows[0].Cells[0].Style.BackColor = Color.Yellow;

I do not want to have the code in this manner. Does anyone know what is happening here that is preventing me from coloring the cells?

like image 803
Nick Avatar asked Sep 20 '12 15:09

Nick


2 Answers

If you try and set the cell colour within the constructor of the form you will be hitting before the data binding is completed so the changes to the cells don't stick (don't ask me why, just one of those gotchas with the DataGridView.

The most straightforward fix to this is to set the colours a little later - usually within a DataBindingComplete event handler:

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    dataGridView1.Rows[0].Cells[0].Style.BackColor = Color.Yellow;
}

This is appropriate for static colouring of the grid - if you want the colours to change according to the changes within the grid then use the CellFormatting event to change the cells.

like image 56
David Hall Avatar answered Oct 31 '22 07:10

David Hall


This Is something I have implemented recently, I dont know if it will help??

private void dgvOutstandingReports_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                int colIndex = e.ColumnIndex;
                int rowIndex = e.RowIndex;


                if (rowIndex >= 0 && colIndex >= 0)
                {
                    DataGridViewRow theRow = dgvOutstandingReports.Rows[rowIndex];


                    if (theRow.Cells[colIndex].Value.ToString() == "Daily Report")
                    {
                        theRow.DefaultCellStyle.BackColor = Color.LightYellow;
                    }
                    else if (theRow.Cells[colIndex].Value.ToString() == "Monthly Report")
                    {
                        theRow.DefaultCellStyle.BackColor = Color.LightGray;
                    }
                    else if (theRow.Cells[colIndex].Value.ToString() == "SMP Report")
                    {
                        theRow.DefaultCellStyle.BackColor = Color.Snow;
                    }
                    else if (theRow.Cells[colIndex].Value.ToString() == "Weekly Report")
                    {
                        theRow.DefaultCellStyle.BackColor = Color.Pink;
                    }
                    else if (theRow.Cells[colIndex].Value.ToString() == "Hourly Report")
                    {
                        theRow.DefaultCellStyle.BackColor = Color.LightSteelBlue;
                    }
                }
            }
like image 22
Derek Avatar answered Oct 31 '22 07:10

Derek