hi,

i would like to change the row color in a data grid view at runtime.

i made this code but it didn't work

foreach (DataGridViewRow dgvr in dgvReservations.Rows)
            {
                string status = dgvr.Cells[10].Value.ToString() ;
                if (status == "Canceled")
                {
                     dgvr.DefaultCellStyle.BackColor = Color.Red;
                }
            }

not even this worked

for(int i = 0; i<dgvReservations.RowCount; i++)
               {
                  string status = dgvReservations.Rows[i].Cells[10].Value.ToString();
                  if (status  == "Canceled")
                  {
                      dgvReservations.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Red;
                  }
              }

Your last method should work, are you sure you're entering the if statement?

yes i done it ! but it didn't work. i worked normally but the colors doesn't change ..

i done the method on form load

Works with me, I did it in constructor too. No problemo.

Check out this code. The 1st part is only a simple dgv population. Then is the code which color the rows:

public Form1()
        {
            InitializeComponent();
            dataGridView1.Columns.Add("col1", "column 1");
            dataGridView1.Columns.Add("col2", "column 2");
            dataGridView1.Rows.Add(1, "Canceled");
            dataGridView1.Rows.Add(2, "Ok");
            dataGridView1.Rows.Add(3, "Canceled");
            dataGridView1.Rows.Add(4, "Canceled");
            dataGridView1.Rows.Add(5, "Ok");

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (dataGridView1["col2", row.Index].Value != null)
                    if (dataGridView1["col2", row.Index].Value.ToString() == "Canceled")
                        row.DefaultCellStyle.BackColor = Color.Red;
            }
        }

It works.

hi, can it be the alternating row color in design effecting this problem ?? since the code those not throw an exception but it will remain the same color as it is

Nope, I have the following and it works fine.

public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();          
            dataGridView1.Rows.Add(5);
            // already added 2 columns in the designer
            this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
            this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor =
                Color.Beige;        
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Red; //works!
        }
    }

thanks ! i made it to work by making the method in form activation instead on form load !!

thanks alot

Hi Guys! Last example work my project! And also i need change color by parametr ,for example if data old --this color may red, if new may green or other please help? Thanks beforly!

Hi.. ..how can you implement it if your using gridview of devex?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.