Click here to Skip to main content
15,891,423 members
Articles / Desktop Programming / Windows Forms
Alternative
Tip/Trick

Change individual DataGridView row colors based on column value

Rate me:
Please Sign up or sign in to vote.
1.80/5 (3 votes)
4 Aug 2011CPOL 42.9K   4   9
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)



Comments and Discussions

 
GeneralMy vote of 5 Pin
rosyp24-Sep-12 9:49
rosyp24-Sep-12 9:49 
GeneralMy vote of 5 Pin
omerelili21-May-12 20:57
omerelili21-May-12 20:57 
GeneralRe: ok Pin
Lakhan Aanjana5-Dec-11 22:46
Lakhan Aanjana5-Dec-11 22:46 
GeneralRe: ok Pin
Lakhan Aanjana5-Dec-11 22:45
Lakhan Aanjana5-Dec-11 22:45 
GeneralI've enabled the feature DataGridView virtualmode And was ta... Pin
mahdi mirkhsravi3-Dec-11 21:36
mahdi 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
mahdi mirkhsravi3-Dec-11 21:18 
GeneralRe: dataGridView1[1, i].Style.BackColor = Color.Red; Pin
Lakhan Aanjana4-Dec-11 22:22
Lakhan 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
Lakhan Aanjana4-Dec-11 22:24 
GeneralRe: try it public void dgBoundItems(object sender, DataGridItem... Pin
Lakhan Aanjana4-Dec-11 22:50
Lakhan Aanjana4-Dec-11 22:50 
try it

public void dgBoundItems(object sender, DataGridItemEventArgs e)
{
foreach(TableCell cell in e.Item.Cells)
{
///e.Item.DataSetIndex = row

///e.Item.Cells.GetCellIndex(cell) = column

if (e.Item.Cells.GetCellIndex(cell) == 2 && e.Item.DataSetIndex > 0)
{


if (Convert.ToInt32(cell.Text.Trim()) < 5000)
{
cell.ForeColor = Color.Red;
}
}
}
}

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.