Change the color of text of a radgrid column...

6 posts, 1 answers
  1. Babu Puchakayala
    Babu Puchakayala avatar
    79 posts
    Member since:
    May 2010

    Posted 19 Sep 2010 Link to this post

    Hi,

    I Created radgrid with the column name "Project Health" ( Unique name is "ProjectHealth"). I can edit this column on popup. Once popup opens they can select text Green or Red or Yellow. (using radcombo box). What i want is when user select the yellow, the text color in the grid should display in yellow and same for both red and green. How to do it. how to do this??
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 20 Sep 2010 Link to this post

    Hello Babu,

    You can do this in OnSelectedIndexChanged event of RadComboBox. Access the column and based on the SelectedItem(color) of RadComboBox, change the text color of column. Sample code is given below for your reference. Change the logic according to your requirement.

    ASPX:
    <telerik:GridTemplateColumn>
        <EditItemTemplate>
            <telerik:RadComboBox ID="RadComboBox1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged">
                <Items>
                    <telerik:RadComboBoxItem Text="--Select--" Value="0" />
                    <telerik:RadComboBoxItem Text="Yellow" Value="1" />
                    <telerik:RadComboBoxItem Text="Red" Value="2" />
                    <telerik:RadComboBoxItem Text="Green" Value="3" />
                </Items>
            </telerik:RadComboBox>
        </EditItemTemplate>
    </telerik:GridTemplateColumn>
    <telerik:GridBoundColumn DataField="ProjectHealth" UniqueName="ProjectHealth" HeaderText="Project Health">
    </telerik:GridBoundColumn>

    C#:
    protected void RadComboBox1_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
       {
           RadComboBox combo = (RadComboBox)o;
           switch (combo.SelectedItem.Text)
           {
               case "Yellow":
                    RadGrid1.MasterTableView.GetColumn("ProjectHealth").ItemStyle.ForeColor = System.Drawing.Color.Yellow;
                   break;
               case "Red":
                   RadGrid1.MasterTableView.GetColumn("ProjectHealth").ItemStyle.ForeColor = System.Drawing.Color.Red;
                   break;
               case "Green":
                   RadGrid1.MasterTableView.GetColumn("ProjectHealth").ItemStyle.ForeColor = System.Drawing.Color.Green;
                   break;
           }
       }

    Thanks,
    Princy.
  3. Babu Puchakayala
    Babu Puchakayala avatar
    79 posts
    Member since:
    May 2010

    Posted 20 Sep 2010 Link to this post

    Thanks Princy Thats working great...
  4. Babu Puchakayala
    Babu Puchakayala avatar
    79 posts
    Member since:
    May 2010

    Posted 20 Sep 2010 Link to this post

    I tried another way .. That is also working ... I am just posting here... If anybody wants... Here is the code...


    protected void gvPjtMnt_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem dataItem = e.Item as GridDataItem;
                TableCell myCell = dataItem["ProjectHealth"];
                myCell.Font.Size = 10;
                myCell.Font.Bold = true;
     
                if (myCell.Text.Trim() == "GREEN")
                {
                    myCell.ForeColor = System.Drawing.Color.Green;
                }
                if (myCell.Text.Trim() == "RED")
                {
                    myCell.ForeColor = System.Drawing.Color.Red;
                }
                if (myCell.Text.Trim() == "YELLOW")
                {
                    myCell.ForeColor = System.Drawing.Color.Yellow;
                }
            }

        }
  5. Luis
    Luis avatar
    11 posts
    Member since:
    Nov 2009

    Posted 14 Jun 2020 in reply to Babu Puchakayala Link to this post

    And how can I change a cell font color based on the value of another colum?

    For example I have a row with 

    Product - Sold

    and I have to change the color of the Product if Sold has 0 or 1 value. 

    Thank you. 

    Luis

  6. Eyup
    Admin
    Eyup avatar
    4046 posts

    Posted 17 Jun 2020 Link to this post

    Hello Luis,

     

    You can use the following approach to change the color:
    https://www.telerik.com/forums/alternatingitemstyle-5e16887f3085#odVQBomUikiIJfrxeBQrww

    And for getting the value of Sold, you can use the DataBinder.Eval approach:
    https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/accessing-values-and-controls/server-side/accessing-cells#accessing-raw-field-data-and-key-values

    Hope this helps.

     

    Regards,
    Eyup
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
Back to Top