c# change color for a row if its not empty

2019-02-28 15:48发布

问题:

I have a table an some values on it. If the Row Cell "Name" is not empty change color of background to violet.

Name    ID    Customers

Niky    1     yes       // here change background to violet
        2     no
Donna   3     yes       // here change background to violet
Baka    4     no        // here change background to violet
        5     yes
        6     no

I have tried this code but i doesnt work, dont know why:

 foreach (DataGridViewRow row1 in dataGridView1.Rows)
        {
            if (row1.Cells[0].Value != null)
            {
                row1.DefaultCellStyle.BackColor = Color.Violet;
            }
        }

回答1:

The usual place to put this sort if code in within the DataBindingComplete event handler, either attach the event as below or using the designer:

dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);

Then in the handler you have something like this:

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow row1 in dataGridView1.Rows)
    {
        if (row1.Cells.Cast<DataGridViewCell>().Any(c => c.Value == null || string.IsNullOrWhiteSpace(c.Value.ToString())))
        {
            row1.DefaultCellStyle.BackColor = Color.Violet;
        }
        else
        {
            row1.DefaultCellStyle.BackColor = Color.White;
        }
    }
}

In the code above I've changed your original code to now looks at all cells rather than just the first.


You can also put the code in the CellFormatting event.



回答2:

A good (but not performant) way for achieving this is the event cell-formatting of the datagridview. You can read the whole documentation in the MSDN. The essence is, that you subscribe to the formatting-event and in this you can do your formatting-issues.

This way you can be sure that the colors etc. are just fine no matter if the user is resizing, scrolling whatever.

Btw: I wouldn't do it in the drawing-event as I learned that usually you will break something of the automated drawing-logic ;)

For further information there is an article about cellstyles in the MSDN which will be in this case overkill. But you can never know enought :)



回答3:

I dont know where you put your colour code but Ive always done it in the drawing section

Heres oen where I coloured the line depending on a status, except for the first column which was dependant on yellow or blue - this is work in progress code and should be tidied up

  private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (e.RowIndex >= 0 && e.ColumnIndex > 0)
        {
            if (dataGridView1.Rows[e.RowIndex].Cells[].Value.ToString() == "good")
            {
                e.CellStyle.BackColor = Color.PaleGreen;
                    e.CellStyle.SelectionForeColor = Color.Black;
                    e.CellStyle.SelectionBackColor = Color.Wheat;

            }
            if (dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString() == "warning")
            {
                e.CellStyle.BackColor = Color.LightGoldenrodYellow;
                e.CellStyle.SelectionForeColor = Color.Black;
                e.CellStyle.SelectionBackColor = Color.Wheat;
            }
            if (dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString() == "error")
            {
                e.CellStyle.BackColor = Color.Salmon;
                e.CellStyle.SelectionForeColor = Color.Black;
                e.CellStyle.SelectionBackColor = Color.Wheat;
            }

                if (e.Value.ToString() == "Yellow")
                {
                    e.CellStyle.BackColor = Color.Yellow;
                }
                else
                    if (e.Value.ToString() == "Blue")
                    {
                        e.CellStyle.BackColor = Color.LightBlue;
                    }


        }
    }

Or you can do:

foreach(DataGridViewRow r in dataGridView1.Rows) 
{ 
      if(!String.IsNullOrEmpty(r.Cells[0].Value.ToString()))
      { 
           r.DefaultCellStyle.BackColor = Color.Violet; 
      } 
} 

so if all the first cell of a line is not empty, colour the row violet.



回答4:

you can do it on the rowdatabound event of the gridview

You can also use this use can go throught it:

foreach(DataGridViewRow r in dataGridView1.Rows)
{
      if(r.Cells.Cast<DataGridViewCell>().Any(c => c.Value.ToString() == string.Empty)) 
      {
           r.DefaultCellStyle.BackColor = Color.Violet;
      }
}