Sunday, September 18, 2011

Changing ASPxGridView Cell and Row Color on Condition

Changing ASPxGridView Cell Color:

To work with this functionality use the ASPxGridView.HtmlDataCellPrepared event. Here you can get values from other fields within the same row using the e.GetValue method. Using this value setup your condition and set the formatting on the cell. using the ASPxGridView ASPxGridViewTableDataCellEventArgs object named e here. e.g.
protected void grvProduct_HtmlDataCellPrepared(object sender, 
            DevExpress.Web.ASPxGridView.ASPxGridViewTableDataCellEventArgs e)
        {
            if (e.DataColumn.FieldName != "Stock") return;
            if (Convert.ToInt32(e.CellValue) < 100)
                e.Cell.BackColor = System.Drawing.Color.Red;
        }
    }

Changing ASPxGridView Row Color:

To work with this functionality use the ASPxGridView.HtmlRowPrepared event.Here you can get values from other fields within the same row using the e.GetValue method. Using this value setup your condition and set the formatting on the row and cell(e.Row.Cell is accessible here) using the ASPxGridView ASPxGridViewTableDataRowEventArgs object named e here.
protected void grvProduct_HtmlRowPrepared(object sender, 
            DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
{
   if (e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.Data) return;
   decimal value = (decimal)e.GetValue("Stock");
   if (value < 100)
       e.Row.ForeColor = Color.Red;
}

2 comments :