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

Set Forecolor where Text contains specific words

3 Answers 95 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ronan BARRANGER
Top achievements
Rank 1
Ronan BARRANGER asked on 05 Mar 2012, 04:29 PM
Greetings,


In my radgrid i have a column displaying "En cours" or " Nouveau" .
This is the column in question:

<telerik:GridBoundColumn DataField="EtatIncident.nomEtatIncident" HeaderText="Etat du ticket"
    SortExpression="Etatduticket" UniqueName="EtatDuTicket">
</telerik:GridBoundColumn>

I need the text "En cours" to be displayed in green

This is my try which unfortunately does not work:

protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
{
    foreach (Telerik.Web.UI.GridDataItem dataItem in RadGrid2.MasterTableView.Items)
    {
        Label lbl = (Label)dataItem.FindControl("EtatDuTicket");
 
       if (lbl.Text == " En cours") { lbl.ForeColor = Color.Green; }
    }
}

Thanks in advance for your help

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 05 Mar 2012, 07:11 PM
Hello,

<telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
                   </telerik:GridBoundColumn>
protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = e.Item as GridDataItem;
                if (item["Name"].Text == "jayesh") // get column value using column unique name
                {
                    item["Name"].Style.Add("Color", "Red");
                            item["Name"].CssClass = ""; // you can also assign css class here
                }
            }
  
}


Thanks,
Jayesh Goyani
0
Accepted
Casey
Top achievements
Rank 1
answered on 05 Mar 2012, 07:41 PM
Hi Ronan,

In the ItemDataBound event you don't need to loop through all of the GridDataItems in the RadGrid because the ItemDataBound event is fired for each item in the RadGrid, as it is bound. 

Jayesh's suggestion should work. You could also set the ForeColor to Color.Green instead of changing the style attribute.

I hope this helps!
Casey
protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem dataItm = e.item as GridDataItem;
        if (dataItm["EtatDuTicket"].Text == " En cours")
        {
           dataItm["EtatDuTicket"].ForeColor = Color.Green;
        }
    }
}

0
Ronan BARRANGER
Top achievements
Rank 1
answered on 06 Mar 2012, 09:33 AM
Thank you to both of you for your advices
Tags
Grid
Asked by
Ronan BARRANGER
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Casey
Top achievements
Rank 1
Ronan BARRANGER
Top achievements
Rank 1
Share this question
or