Click here to Skip to main content
15,887,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want with a condition : * all rows have bool_badge =0 : color with RED * all rows have bool_badge=1 : color with ForestGreen

I have a code Correct BUT just when i click for a cell specific:




But i have result just when i click in a specific ROW

But I want when i execute my application , the test begin if bool_badge 0 or 1, and i have for all the gridview : color RED or ForestGreen ,


I try this code:


But i have ERROR!

in this ligne :
C#
string valeur = dataGridView1[2, i].Value.ToString();


The NullReferenceException was unhandled

Object reference not set to an instance of an object


How can i fix it?

Very thanks,

What I have tried:

* Code for just a specific ROW:
C#
foreach (DataGridViewRow dr in dataGridView1.Rows)
           {
               int row = this.dataGridView1.CurrentCell.RowIndex;
               string valeur = dataGridView1[2, row].Value.ToString();

               if (valeur == "0")
               {
                   dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red;
               }
               else
               {
                   dataGridView1.DefaultCellStyle.SelectionBackColor = Color.ForestGreen;
               }
           }



* My try code:
C#
for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {                
            string valeur = dataGridView1[2, i].Value.ToString();

            if (valeur == "0")
            {
                dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red;
            }
            else
            {
                dataGridView1.DefaultCellStyle.SelectionBackColor = Color.ForestGreen;
Posted
Updated 2-Nov-16 4:49am
v3

Handle the DataGridView.RowPrePaint Event (System.Windows.Forms)[^] and do it in there. The DataGridViewRowPrePaintEventArgs parameter contains the RowIndex which lets you decide what colour to paint the background.
 
Share this answer
 
I find the Coreect code,

with use Datagridview Cell_Formatting event



C#
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].HeaderText == "bool_badge" && dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null) 
         // if the column is bool_badge and check null value for the extra row at dgv
            {
                    if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "0")
                    {
                        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
                    }
                    if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "1")
                    {
                        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.ForestGreen;
                    }
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900