15 Answers, 1 is accepted
With Q1 2010 you are able to apply conditional styles and templates by using DataTemplateSelectors and StyleSelectors for your RadGridView.
I am sending you a sample project that achieves the result you want - to change the background of a cell according to its value. However, you may find additional information on using both DataTemplateSelectors and StyleSelectors in this blog post.
Maya
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.
If you want to change the background, you can work with StyleSelectors. Check out this article and this demo (here the rules for selecting a style are all created in xaml) for more information.
Maya
Telerik
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
Can you show me how to define the prefix my in the xaml file? I don't have a namespace defined in my vb code since the example didn't either. Is a namespace required? I tried some namespaces but I still cannot get the xaml to recognize the my prefix. I read the article on namespaces but no luck. Thanks
This prefix is defined in xaml to point to the namespace of the current project:
xmlns:my="clr-namespace:ConditionalFormatting"
If you are using it for accessing your StyleSelector, it should point to the place where it is defined (its namespace).
Maya
Telerik
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
http://demos.telerik.com/silverlight/#GridView/Selectors/StyleSelectors/DeclarativeDefinition
However, I can't get the BasedOn to work. The resource 'GridViewCellStyle' could not be resolved.
What am I missing for that resource?
I am using version: 2012.1.215.1050
Thanks
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:telerikGridView="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
BasedOn="{StaticResource GridViewCellStyle}">
I would recommend you to run through this article for a reference on how to define custom xaml namespace and resolve the first issue you were having.
As for the style, you could take a look at our documentation for more information. If you are working with normal (reather than with implicit themes and noXaml binaries), you do not need to base your style on the default one.
Maya
Telerik
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
As I am pivoting my data I am adding a column for each day between a start and end date at runtime (in the dataloaded handler) as follows:
foreach (DateTime day in EachDay(_startDate, (DateTime)_endDate))
{
columns.Add(new GridViewExpressionColumn() { Header = day.ToString("dd MMM yyyy"), UniqueName = day.ToString("dd MMM yyyy"), DataFormatString = "{0:##,###}", IsReadOnly = true });
Expression<Func<FUNDING_TRADES, decimal?>> consideration = row => writeConsideration(row, day);
GridViewExpressionColumn considerationExpression = this.radGridView.Columns[day.ToString("dd MMM yyyy")] as GridViewExpressionColumn;
considerationExpression.Expression = consideration;
}
Then I want to loop through the columns and foreach date column format weekends
DateTime columnDate;
foreach (Telerik.Windows.Controls.GridViewColumn column in radGridView.Columns)
{
if (column != null && DateTime.TryParse(column.Header.ToString(), out columnDate))
{
if(columnDate.DayOfWeek == DayOfWeek.Saturday || columnDate.DayOfWeek == DayOfWeek.Sunday)
{
//applyweekend format
}
else
{
//apply week format
}
........
How can I do this programatically?
I've been looking at using the ConditionalStyleSelector and stylerules but haven't been able to get it to work
thanks
To achieve your goal you should be using CellStyleSelector. Please check the above documentation article for further reference. However, I set up a sample project for your convenience.
Let me know if you need any further assistance.
Kind Regards,
Stefan
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
thanks for the project.
Unfortunately that doesn't work for me.
I have seen that example before.
I am adding columns at runtime as I don't know the number of columns I will need (is one per day between a start and end date determined at runtime).
columns.Add(new GridViewExpressionColumn() { Header = day.ToString("dd MMM yyyy"), UniqueName = day.ToString("dd MMM yyyy"), DataFormatString = "{0:##,###}", IsReadOnly = true });
So I can't define the CellStyleSelector in XAML at design time.
I need a way to be able to add a CellStyleSelector or some other method at runtime once I have programatically created the columm and added it to the columns collection.
Hope that makes sense,
Eliot
If I am getting you right you want to have different Style for the weekdays and the weekend ones. If that is your goal, then CellStyleSelector is exactly what you need. The number of columns is irrelevant to this case.You will just need to define your CellStyleSelector in XAML and set it programmatically to the dynamically created columns. You could check programmatically if a particular day is from the workweek or from the weekend and simply return the Style according to that day.
I have modified the sample project in a way that it reproduces the above scenario.
I hope that this helps.
Kind Regards,
Stefan
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
I ended up using CellStyle.
In my App.xaml I declared styles as follows:
<Application.Resources>
<Style TargetType="{x:Type telerik:GridViewCell}" x:Key="WeekEndStyle">
<Setter Property="Background" Value="LightGray" />
</Style>
<Style TargetType="{x:Type telerik:GridViewCell}" x:Key="WeekDayStyle">
<Setter Property="Background" Value="White" />
</Style>
Then in radGridView_DataLoaded I assign the style resource to CellStyle
if (day.DayOfWeek == DayOfWeek.Saturday || day.DayOfWeek == DayOfWeek.Sunday)
{
dateColumn.CellStyle = Application.Current.Resources["WeekEndStyle"] as Style;
}
else
{
dateColumn.CellStyle = Application.Current.Resources["WeekDayStyle"] as Style;
}
Regards,
Eliot
I am glad that you have found solution suitable for your needs.
We will be glad to provide further assistance if it is needed.
Regards,
Stefan
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.