You must Sign In to post a response.
  • Category: General

    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
  • #756975
    Your code seems fine so you need to debug your code and check in which condition it is going everytime. I think in you code
    int warrenty = Convert.ToInt32(p.Warrenty_Status);
    line return same value always so that will be set same color for whole rows in grid.

    Regards & thanks
    Arvind kumar
    Visit--blog.akumars.esoftera.in

  • #756978
    Hi,

    Please refer below code snippet to resolve your issue:

    private void dgvPDFContent_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
    for (int i = 0; i < dgvPDFContent.Rows.Count - 1; i++)
    {
    if (dgvPDFContent.Rows[i].Cells[23].Value.ToString() == "Manager")//Set your condition here
    {
    dgvPDFContent.Rows[i].DefaultCellStyle.BackColor = Color.SkyBlue;//Set your color here
    }
    }
    }

    Hope it will be helpful to you.

    Thanks,
    Ram Prasad

  • #756986
    Is it windows or webbased application ? for web based application you can use 'rowdatabound' event and change the row color there
    check out following snippet

    protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    //check some condition
    if (Convert.ToInt32(textbox1.Text) > 100)
    {
    e.Row.ForeColor = System.Drawing.Color.Black;
    e.Row.Font.Bold = true;
    e.Row.BackColor = System.Drawing.Color.Brown;
    }
    }
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • Sign In to post your comments