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

RadGrid Cell Formatting in ASP.NET

4 Answers 488 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jamil
Top achievements
Rank 1
Jamil asked on 15 Mar 2010, 11:16 PM
Hi,
I've set the data source of the RadGrid dynamically. Now I'm trying to set the cell formatting of the RadGrid because in my case i've to set different formatting (bold, italic, underline etc) of the each grid cell. For example, i want to set the font bold to true for the only first row and first column which will be basically first cell of the grid. i.e.

I've tried to do using the following code but in vain. One more thing, i'm not able to see the cell value at this point. I've tried both the ItemCreated and ItemDataBoudn event but nothing is working for me.

protected

 

void RadGridComparableAnalysis_ItemDataBound(object sender, GridItemEventArgs e) {

 

      

if (e.Item is GridDataItem) {

 

        e.Item.Cells[0].Font.Bold =

true
    }
}

 

 

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 16 Mar 2010, 05:34 AM

Hello Jamil,

You can access the column using the ColumnUniqueName property adn set the style as shown below.

C#:

 
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)  
    {  
        if (e.Item is GridDataItem)  
        {  
            GridDataItem item = (GridDataItem)e.Item;  
            if (item.ItemIndex == 0)   // Get the first item
            {  
                item["Age"].Style.Add("font-weight""bold");  
            }  
        }  
    } 

-Shinu.

0
Jamil
Top achievements
Rank 1
answered on 16 Mar 2010, 03:17 PM
Hi Shinu,

Your suggested code did execute without any error but It is not applying the style. When i debugged the code and tried to see the cell value then i found that it is " " instead of the actual value.

It is surprize for me because i did setup the data source property of the grid and then call the DataBind() method but when control gets into the ItemDataBound event i'm not able to get the actual cell value. 

Please advise

Thanks

 
0
Veli
Telerik team
answered on 19 Mar 2010, 07:51 AM
Hi Jamil,

Try it back with your original code, but note that you need to access the 3rd cell, not the first. This is because RadGrid always creates 2 columns that may not be visible. Therefore, the first actual data cell is the third cell in each data item:

protected void RadGridComparableAnalysis_ItemDataBound(object sender, GridItemEventArgs e)
{    
    if (e.Item is GridDataItem)
    {
        e.Item.Cells[2].Font.Bold = true;
    }
}


Kind regards,
Veli
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Jim Foster
Top achievements
Rank 1
answered on 14 Jun 2012, 04:01 PM
This may help someone else in the future that wants to do the same, but by column name instead of index number.  In the sample below, HistoryItem is an internal custom class.

protected void HistoryRadGrid_ItemCreated(object sender, GridItemEventArgs e)
{
    GridDataItem dataItem;
    HistoryItem historyItem;
 
    dataItem = e.Item as GridDataItem;
    if (dataItem != null)
    {
        historyItem = dataItem.DataItem as HistoryItem;
        if (historyItem != null)
        {
            if (historyItem.Status == "Pending")
            {
                // dataItem.Font.Italic = true; // This will affect the entire row
                dataItem["Price"].Font.Italic = true;
                dataItem["Status"].Font.Italic = true;
            }
        }
    }
}

Tags
Grid
Asked by
Jamil
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Jamil
Top achievements
Rank 1
Veli
Telerik team
Jim Foster
Top achievements
Rank 1
Share this question
or