Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Change individual DataGridView row colors based on column value

0.00/5 (No votes)
4 Aug 2011 1  
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;
                }
            }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here