How to change gridview row color based on condition
Are you looking for a way to change gridview row color based on condition ? then read this thread to know more about it
Hi, I am using the following codes to change the color of gridview row in c# windows application. It's a project developed using the wcf web services. Following codes are assigning the same type of colors for the whole rows of gridview whereas I want to dispaly different colors based on conditions. Please let me know that what step I m missing. Please assist.
protected void databindProducts()
{
CMS.ServiceReference1.Product[] res = CMSGlobals.service.GetAllProduct();
dgvProducts.DataSource = res;
foreach (DataGridViewRow row in dgvProducts.Rows)
{
foreach (CMS.ServiceReference1.Product p in res)
{
int warrenty = Convert.ToInt32(p.Warrenty_Status);
if (row.DataBoundItem.Equals(p))
{
if (warrenty < 15)
{
// dgvProducts.RowsDefaultCellStyle = new DataGridViewCellStyle();
dgvProducts.RowsDefaultCellStyle.BackColor = Color.Red;
//dgvProducts.RowsDefaultCellStyle.SelectionBackColor = Color.LightBlue;
//row.DefaultCellStyle.BackColor = Color.Green;
}
else if (warrenty < 30 && warrenty >= 15)
{
dgvProducts.RowsDefaultCellStyle.BackColor = Color.Brown;
}
else if (warrenty >= 30)
{
dgvProducts.RowsDefaultCellStyle.BackColor = Color.Gold;
}
else
{
dgvProducts.RowsDefaultCellStyle.BackColor = Color.Green;
}
}
}
}
}
Regards,
DP Sharma
