Click here to Skip to main content
14,664,481 members
Articles » Languages » C# » General
Alternative
Tip/Trick
Posted 4 Aug 2011

Tagged as

Stats

39.8K views
4 bookmarked

Change individual DataGridView row colors based on column value

Rate this:
1.80 (3 votes)
Please Sign up or sign in to vote.
1.80 (3 votes)
4 Aug 2011CPOL
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
GeneralMy vote of 5 Pin
rosyp24-Sep-12 9:49
Memberrosyp24-Sep-12 9:49 
GeneralMy vote of 5 Pin
omerelili21-May-12 20:57
Memberomerelili21-May-12 20:57 
GeneralRe: ok Pin
Lakhan Aanjana5-Dec-11 22:46
MemberLakhan Aanjana5-Dec-11 22:46 
GeneralRe: ok Pin
Lakhan Aanjana5-Dec-11 22:45
MemberLakhan Aanjana5-Dec-11 22:45 
GeneralI've enabled the feature DataGridView virtualmode And was ta... Pin
mahdi mirkhsravi3-Dec-11 21:36
Membermahdi mirkhsravi3-Dec-11 21:36 
GeneralI use this code but not : for (int i = 0; i &lt; dstTempera... Pin
mahdi mirkhsravi3-Dec-11 21:18
Membermahdi mirkhsravi3-Dec-11 21:18 
GeneralRe: dataGridView1[1, i].Style.BackColor = Color.Red; Pin
Lakhan Aanjana4-Dec-11 22:22
MemberLakhan Aanjana4-Dec-11 22:22 
GeneralRe: what is this ?fist u set red and than white remove one //at... Pin
Lakhan Aanjana4-Dec-11 22:24
MemberLakhan Aanjana4-Dec-11 22:24 
GeneralRe: try it public void dgBoundItems(object sender, DataGridItem... Pin
Lakhan Aanjana4-Dec-11 22:50
MemberLakhan Aanjana4-Dec-11 22:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.