How to change background color of RADGRID control row based the column values?

Posted on 2011-10-27
Last Modified: 2012-05-12
I am using Telerik RadGrid control to display display from a dataset.
Based on the data in a particular column of the row I would like to display different background color for different rows.
Let me say for example I have column called MARKS in my grid, if this column in a row null, then the I want that row background color to be RED.
How ca I do this? Where should I bind this event?
Question by:beginner_help
    LVL 12

    Accepted Solution

    Hope this may help you:
    protected void RadGrid1_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
         //Is it a GridDataItem
         if (e.Item is GridDataItem)
               //Get the instance of the right type
               GridDataItem dataBoundItem = e.Item as GridDataItem;

               //Check the formatting condition
                 if (int.Parse(dataBoundItem["Marks"].Text) == null )
                     dataBoundItem[ "Data"].ForeColor = Color.Red;
                     dataBoundItem[ "Data"].Font.Bold = true;
                     //Customize more...

    Using CSS:

    protected void RadGrid1_ItemDataBound(Object sender, Telerik.WebControls.GridItemEventArgs e)
            if (e.Item is GridDataItem)
               GridDataItem dataItem = e.Item as GridDataItem;
                if (dataItem["Country"].Text = "Mexico")
                   dataItem.CssClass = "MyMexicoRowClass";


    Author Comment

    In my RADGRID I am displaying the value in the grid using LABEL to display the value.
    So when I am using if (int.Parse(dataBoundItem["NBR"].Text) == null ), it always returning since the dat is in the label.
    I tried to use FINDCONTROL to find find the label, but it is throwing exception.
    Label lbl=(Label)FindControl("lblNbr");

    <telerik:RadGrid AllowPaging="true" ID="grdQueues" runat="server" OnSelectedIndexChanged="grd_SelectedIndexChanged"  
                            ShowStatusBar="true" AllowSorting="true" PageSize="10" AutoGenerateColumns="false" 
                            OnPageIndexChanged="grd_PageIndexChanged" OnNeedDataSource="grd_NeedDataSource"
                            Width="100%" Visible="false" OnExcelMLExportStylesCreated="grd_ExcelMLExportStylesCreated"
                        <PagerStyle AlwaysVisible="true" Mode="NextPrevAndNumeric" PagerTextFormat="Change page: {4} | Displaying page {0} of {1} |, items {2} to {3} of {5}." />
                        <ClientSettings EnableRowHoverStyle="true" EnablePostBackOnRowClick="true">
                            <Selecting AllowRowSelect="true" />
                            <Scrolling AllowScroll="true" UseStaticHeaders="true" />                        
                        <MasterTableView ShowHeadersWhenNoRecords="true" EditMode="PopUp">      
                            <NoRecordsTemplate><div align="center">
                                    No Records to Display
                                <telerik:GridTemplateColumn HeaderText="Acct Nbr">
                                        <asp:Label ID="lblNbr" ClientIDMode="Static" runat="server" Text='<%# Bind("NBR") %>'></asp:Label>
                                    <ItemStyle HorizontalAlign="Center" />
                                        <HeaderStyle HorizontalAlign="Center" Font-Bold="true" />
                                <telerik:GridTemplateColumn HeaderText="Customer Name">
                                        <asp:Label ID="lblName" ClientIDMode="Static" runat="server" Text='<%# Bind("NAME") %>'></asp:Label>
                                    <ItemStyle HorizontalAlign="Center" />
                                        <HeaderStyle HorizontalAlign="Center" Font-Bold="true" />

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    C# ConsolApp opens new window 16 43
    C# Winform panel within a panel 4 31
    Aspnet xml 2 27
    Xamarin 10 18
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now