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

    How to insert the colour in gridview's ros conditionally

    Hi,
    I am trying to give the row colour in Gridview conditionally, but the following codes are working for the whole grid. Please assist to change the gridview's row colour as per given condition.
    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)
    {
    if (row.DataBoundItem.Equals(p))
    {
    if (Convert.ToInt32(p.Warrenty_Status) < 365)
    {
    dgvProducts.RowsDefaultCellStyle = new DataGridViewCellStyle();
    dgvProducts.RowsDefaultCellStyle.BackColor = Color.Red;
    dgvProducts.RowsDefaultCellStyle.SelectionBackColor = Color.Red;

    }
    }
    }
    }
    Also, I want to open the form while double click on a row in above gridview. Please assist.
  • #756892
    Hi DP Sharma,

    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() == "Recruited")
    {
    dgvPDFContent.Rows[i].DefaultCellStyle.BackColor = Color.SkyBlue;
    }
    }
    }

    Hope it will be helpful to you.

    Thanks,
    Ram Prasad

  • #756904
    Yes you can do this in rowdatabound event of gridview to show different color based on condition
    eg
    protected void DrugGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    If(OrderStatus == "1") //here you can use your condition which is required
    {
    e.Row.BackColor = Drawing.Color.Red ;
    }
    else
    {
    e.Row.BackColor = Drawing.Color.Green;
    }
    }
    }

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

  • #758991
    gridview.Rows[i].BackColor = Color;


  • Sign In to post your comments