65.9K
CodeProject is changing. Read more.
Home

Change individual DataGridView row colors based on column value

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.80/5 (3 votes)

Aug 4, 2011

CPOL
viewsIcon

43646

If cell color is depend on criteriaif (ca.con.State == ConnectionState.Closed) { ca.con.Open(); } string qry = "select * From productmaster "; SqlDataAdapter da = new SqlDataAdapter(qry, ca.con); DataSet ds1 =...

If cell color is depend on criteria
if (ca.con.State == ConnectionState.Closed)
            {
                ca.con.Open();
            }
            string qry = "select * From productmaster ";
            SqlDataAdapter da = new SqlDataAdapter(qry, ca.con);
            DataSet ds1 = new DataSet();
            da.Fill(ds1, "productmaster");
            for (int i = 0; i < ds1.Tables["productmaster"].Rows.Count; i++)
            {
                int val = Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value);// .SelectedItem.Cells[1].Text;
                if (val == 0)
                {
                    //dataGridView1.Rows[i].Cells[4].Style.BackColor = Color.Red;
                    dataGridView1.Rows[i].Cells[4].Style.ForeColor = Color.Red;
                }
                else
                {
                    dataGridView1.Rows[i].Cells[4].Style.ForeColor = Color.Green;
                }
            }