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

Posted on 2011-10-27
Medium Priority
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

Praveen Kumar earned 2000 total points
ID: 37044057
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

ID: 37052896
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


Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

757 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