DataGridView changing cell background color

2020-01-27 06:46发布

I have the following code :

private void dgvStatus_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow row in dgvStatus.Rows)
    {
        row.Cells[color.Index].Style.BackColor = Color.FromArgb(((GesTest.dsEssais.FMstatusAnomalieRow)row.DataBoundItem).iColor);
    }
}

I am trying to set the background color of each cell from the background color column. this doesn't work the color never change. Any idea of why?

I've been looking around but didn't found anything usefull

10条回答
相关推荐>>
2楼-- · 2020-01-27 06:52

Similar as shown and mentioned:

Notes: Take into consideration that Cells will change their color (only) after the DataGridView Control is Visible. Therefore one practical solution would be using the:

VisibleChanged Event

In case you wish to keep your style when creating new Rows; also subscribe the:

RowsAdded Event


Example bellow:

///<summary> Instantiate the DataGridView Control. </summary>
private DataGridView dgView = new DataGridView;

///<summary> Method to configure DataGridView Control. </summary>
private void DataGridView_Configuration()
{
    // In this case the method just contains the VisibleChanged event subscription.

    dgView.VisibleChanged += DgView_VisibleChanged;

    // Uncomment line bellow in case you want to keep the style when creating new rows.
    // dgView.RowsAdded += DgView_RowsAdded;
}

///<summary> The actual Method that will re-design (Paint) DataGridView Cells. </summary>
 private void DataGridView_PaintCells()
 {
     int nrRows = dgView.Rows.Count;
     int nrColumns = dgView.Columns.Count;
     Color green = Color.LimeGreen;

     // Iterate over the total number of Rows
     for (int row = 0; row < nrRows; row++)
     {
         // Iterate over the total number of Columns
         for (int col = 0; col < nrColumns; col++) 
         {
             // Paint cell location (column, row)
             dgView[col, row].Style.BackColor = green;
         }
     }
 }

///<summary> The DataGridView VisibleChanged Event. </summary>
private void DgView_VisibleChanged(object sender, EventArgs e)
{
    DataGridView_PaintCells();
}

/// <summary> Occurrs when a new Row is Created. </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DgView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{ 
    DataGridView_PaintCells(); 
}


Finally: Just call the DataGridView_Configuration() (method)
i.e: On Form Load).

查看更多
来,给爷笑一个
3楼-- · 2020-01-27 06:53

If you are still intrested in why this didn't work for you at first:

The reason you don't see changes you've made to the cell's style is because you do these changes before the form was shown, and so they are disregarded.

Changing cell styles in the events suggested here will do the job, but they are called multiple times causing your style changes to happen more times than you wish, and so aren't very efficient.

To solve this, either change the style after the point in your code in which the form is shown, or subscribe to the Shown event, and place your changes there (this is event is called significantly less than the other events suggested).

查看更多
Rolldiameter
4楼-- · 2020-01-27 06:55

Simply create a new DataGridViewCellStyle object, set its back color and then assign the cell's style to it:

    DataGridViewCellStyle style = new DataGridViewCellStyle();
    style.BackColor = Color.FromArgb(((GesTest.dsEssais.FMstatusAnomalieRow)row.DataBoundItem).iColor);
    style.ForeColor = Color.Black;
    row.Cells[color.Index].Style = style;
查看更多
SAY GOODBYE
5楼-- · 2020-01-27 06:58

You can use the VisibleChanged event handler.

private void DataGridView1_VisibleChanged(object sender, System.EventArgs e)
{
    var grid = sender as DataGridView;
    grid.Rows[0].Cells[0].Style.BackColor = Color.Yellow;
}
查看更多
何必那么认真
6楼-- · 2020-01-27 07:01
int rowscount = dataGridView1.Rows.Count;         

for (int i = 0; i < rowscount; i++)
{            
    if (!(dataGridView1.Rows[i].Cells[8].Value == null))
    {
        dataGridView1.Rows[i].Cells[8].Style.BackColor = Color.LightGoldenrodYellow;
    }
}
查看更多
再贱就再见
7楼-- · 2020-01-27 07:05

I finally managed to get it working. Here the code :

private void dgvStatus_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex != color.Index)
        return;

    e.CellStyle.BackColor = Color.FromArgb(int.Parse(((DataRowView)dgvStatus.Rows[e.RowIndex].DataBoundItem).Row[4].ToString()));
}

if anyone know a better to do this please don't hesitate to post it. I'm open to suggestion

查看更多
登录 后发表回答