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

Conditional formatting based on Cell.Content

3 Answers 179 Views
GridView
This is a migrated thread and some comments may be shown as answers.
NS
Top achievements
Rank 1
NS asked on 04 Feb 2010, 08:38 AM
Hi,
I have a situation where I want to apply the conditional formatting example.

I have 52 cells in a row that represent a week number in a year.
This is represented in my business object as follow: Week1.days, Week2.days, Week3.days, etc

I don't know how to set the Binding CellFgBinding = new Binding("Week1.days");

In the RowLoaded event, I think I also must loop all the cells in the row, to get the relevant week, something like this:

private

 

void RadGridView_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)

 

{

 

if (e.Row is GridViewRow)

 

{

 

 

capacityCheckSummary CCS = (capacityCheckSummary)e.Row.DataContext;

 

 

if (CCS.summary == "D Capacity %")

 

{

 

foreach (GridViewCell C in e.Row.Cells)

 

{

 

Binding CellFgBinding = new Binding("I don't know the binding");

 

CellFgBinding.Converter =

new percentageConverter();

 

e.Row.Cells.Last().SetBinding(

GridViewCellBase.BackgroundProperty, CellFgBinding);

 

}

 

}

}

Is there a way to avoid the Binding and get the current Cell.Content and pas it to the converter ?

3 Answers, 1 is accepted

Sort by
0
Tsvyatko
Telerik team
answered on 05 Feb 2010, 06:06 PM
Hi NS,

I can suggest you using the following code similar to yours:
if (e.Row is GridViewRow)
{
    for (int i = 0; i < e.Row.Cells.Count; i++)
    {
        Binding CellFgBinding = new Binding(".");
        CellFgBinding.Converter = new WeekToAvailablilityConverter();
        CellFgBinding.ConverterParameter = i;
        e.Row.Cells[i].SetBinding(GridViewCellBase.ForegroundProperty, CellFgBinding);
    }
}

That way you can pass to the ValueConverter as parameter additional information to return the value you need. I can also suggest you to put all weeks in a list, rather than using properties.

Another option is to build binding expression like this : new Binding("Week"+ i +".days");

I hope this helps.

Is you have any other questions or issues do not hesitate to contact us.


Regards,
Tsvyatko
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
preeti
Top achievements
Rank 1
answered on 14 Mar 2011, 11:34 AM
Hi i have a similar requirement but the formatting is  not applied. can you tell me where i am going wrong.I am using Q3 version binaries.The Condition is loaded dynamically. it is something like If Col value is "Y" then set cell background color to Blue.
public class BackColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            ColorRuleConfig obj = parameter as ColorRuleConfig;
            string[] strarr = obj.RuleExpression.Split(new char[] { '=' });
            string strColName = strarr[0];
            string strColVal = strarr[1];
            string str = value as string;
            if (String.IsNullOrEmpty(str))
            {
                return string.Empty;
            }
            try
            {
                if (str == strColVal)
                {
                    return obj.RuleBackColor.ToString();
                }
                else
                {
                    return string.Empty;
                }
            }
            catch
            {
            }
            return str;
        }
  
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return string.Empty;
        }
    }
  
  
 private void dataGrid1_RowLoaded(object sender, RowLoadedEventArgs e)
        {
             Dictionary<string, ColorRuleConfig> dictColorRuleConfig = viewmodelobj.dictColorRuleConfig;
             if (dictColorRuleConfig != null && dictColorRuleConfig.Count > 0)
             {
                 foreach (var item in dictColorRuleConfig)
                 {
                     if (e.Row is GridViewRow)
                     {                        
                        Binding CellBgBinding = new Binding(item.Value.ColumnName.ToString());
                        CellBgBinding.Converter = new BackColorConverter();
                        CellBgBinding.ConverterParameter = item.Value;
                        int index = dataGrid1.Columns[item.Value.ColumnName].DisplayIndex;
                        e.Row.Cells[index].SetBinding(GridViewCellBase.BackgroundProperty, CellBgBinding);                         
                     }
                 }
             }
        }      
0
Vanya Pavlova
Telerik team
answered on 14 Mar 2011, 01:44 PM
Hello preeti,

 
Please use the following forum thread.

All the best,
Vanya Pavlova
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
GridView
Asked by
NS
Top achievements
Rank 1
Answers by
Tsvyatko
Telerik team
preeti
Top achievements
Rank 1
Vanya Pavlova
Telerik team
Share this question
or