This is a migrated thread and some comments may be shown as answers.

Conditionally cell formatting ALL cells

1 Answer 96 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Allan
Top achievements
Rank 2
Allan asked on 02 Oct 2014, 09:18 PM
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?

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 07 Oct 2014, 11:38 AM
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">
    <MasterTableView>
        <Columns>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

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.


Regards,
Konstantin Dikov
Telerik
 

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.

 
Tags
Grid
Asked by
Allan
Top achievements
Rank 2
Answers by
Konstantin Dikov
Telerik team
Share this question
or