Conditionally cell formatting ALL cells

2 posts, 0 answers
  1. Allan
    Allan avatar
    121 posts
    Member since:
    Jun 2008

    Posted 02 Oct 2014 Link to this post

    I have a radgrid that displays number values'

    Is it possible to scan the entire grid and highlight all cells who's value is great than say 100?
  2. Konstantin Dikov
    Konstantin Dikov avatar
    2115 posts

    Posted 07 Oct 2014 Link to this post

    Hi Allan,

    For achieving such result you could handle the server-side OnPreRender event of the grid, traverse through each GridDataItem and each TableCell of the item's Cells collection and using the value of the cell, determine whether or not to change styles within the cell.

    The following example will change the background color of all cells holding a value greater than 100:
    <telerik:RadGrid runat="server" ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" OnPreRender="RadGrid1_PreRender">

    And the code-behind:
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        DataTable table = new DataTable();
        table.Columns.Add("Column1", typeof(int));
        table.Columns.Add("Column2", typeof(int));
        table.Columns.Add("Column3", typeof(int));
        table.Columns.Add("Column4", typeof(int));
        table.Rows.Add(1, 10, 50, 120);
        table.Rows.Add(1, 110, 50, 11);
        table.Rows.Add(1, 10, 50, 1);
        table.Rows.Add(1, 140, 520, 3);
        (sender as RadGrid).DataSource = table;
    protected void RadGrid1_PreRender(object sender, EventArgs e)
        foreach (GridDataItem item in RadGrid1.Items)
            foreach (TableCell cell in item.Cells)
                double value;
                if (double.TryParse(cell.Text, out value))
                    if (value > 100)
                        cell.BackColor = System.Drawing.Color.Goldenrod;

    Hope this helps.

    Konstantin Dikov

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

Back to Top