DataGridView changing cell background color

后端 未结 10 1632
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 10:20

I have the following code :

private void dgvStatus_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow          


        
相关标签:
10条回答
  • 2020-11-30 10:56

    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).

    0 讨论(0)
  • 2020-11-30 10:56
    dataGridView1.Rows[i].Cells[7].Style.BackColor = Color.LightGreen;
    
    0 讨论(0)
  • 2020-11-30 10:56
    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 10:57

    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

    0 讨论(0)
提交回复
热议问题