I have a RadGridView which is bound to a DomainDataSource (RIA Services). The User is be able to Add, Edit, Clone and Delete rows. The DomainDataSource helps me keep track of the entity changes, but I would like to also "keep track" visually in the grid. In other words, I would like to change the text color of the entire row for each action the user performs. In case of an Add, the text color must be blue, in case of an Edit, it must be green, etc.
I have tried applying a StyleSelector via the RowStyleSelector property (As per Telerik Documentation), but it doesn't appear to update after User actions occured.
Is there a way to access it each Row and apply a given style to it? Or is there an alternative way to handle this?
Please advise,
Stefan Buys
18 Answers, 1 is accepted
Please check this blog post for an example and source code:
Displaying live streaming data with RadGridView for Silverlight and WPF
Sincerely yours,
Veselin Vasilev
the Telerik team
The blog post you referred me to shows how the CellTemplate associated to each column within the row gets updated dynamically from the underlying ItemsSource. I want to avoid updating each column's CellTemplate within the row. I simply want to be able to update a common property of the entire row. For instance, I subscribed to the RowEditEnded event handler which exposes a GridViewRow and was able to simply perform:
...
if
(((UsersEntity)e.Row.Item).EntityState == EntityState.New)
e.Row.Foreground =
new
SolidColorBrush(Colors.Blue);
...
if
(((UsersEntity)e.Row.Item).EntityState == EntityState.Modified)
e.Row.Foreground =
new
SolidColorBrush(Colors.Green);
However, as soon as I inserted a new row, the previously updated/added rows's Foreground color resets. As a workaround, I tried looping through the Rows collection within the grid and set the Foreground color based on the Entity's state, but there appears to be no property that contains a collection of GridViewRows (similar to the GridViewColumnCollection). I even tried the above code in conjuction with a RowStyleSelector performing the same actions, but the Style updates occurs randomly and incorrectly.
Any further advice will be most welcome.
Stefan
Please find attached a sample project.
It uses the Employees domain service to connect to the Employees table of the Northwind database.
I have added one additional field to the table - IsNew. I set it to True for every new created entity (look in the Employee.cs file in the Silverlight project). I have created a RowStyleSelector which changes the background for every item marked as new (IsNew = true).
I hope it will get you started.
All the best,
Veselin Vasilev
the Telerik team
I wanted to use a rowstyleselector to set the colour of the grid column based on a property of my entity. I've done something very similar to your example and the colour displays fine.
I have a field called IsValid that I use to set the grid row selector that gets updated based on values in other fields. When I change the fields in the grid to make the record valid the IsValid the field updates in the grid but the rowselector doesn't change colour.
How do I reapply the row style when this gets modified?
It should be enough to raise the PropertyChangedEvent (form the INotifyPropertyChanged Interface) when the IsValid field is changed. In case this does not do the trick , you may need to upgrade to a later version as in early version s of RadGridView StyleSelectors did not react on changes on-the-fly. Currently this is fixed and the INotifyPropertyChanged approach should do the trick .
Regards,
Pavel Pavlov
the Telerik team
I am firing a property change as you say and I'm running the latest service pack.
We've just fixed a bug related to this and the fix will be part of both our latest build and our Q3 2010 release.
Greetings,Vlad
the Telerik team
I downloaded the sample. And it works fine until I try to set AlternateRowBackground="Bisque" AlternationCount="2". In this case no style applies for alternate row. How can I avoid it?
Thanks,
Juliana
I am going to check the issue. Which example are you refering ?
Regards,
Pavel Pavlov
the Telerik team
I meant ria-rowstyleselector.zip by Veselin Vasilev. I changed control version to 2010.3.1110.1040, set AlternateRowBackground="Bisque" AlternationCount="2" and custom style disappeared for alternate rows.
Thanks,
Juliana
Since the AlternateRow style is applied after the styling of the other GridViewRows, it overrides the conditionally set styles of your StyleSelector. I am sending you a workaround that utilizes the StyleSelector to set styles on your AlternationRows, instead of RadGridView's AlternationStyle logic. Please, let me know if this approach fits into you requirements.
Sincerely,
the Telerik team
event on that item's property. Below is my code:
---------------------------------------------------------
--StyleSelector
---------------------------------------------------------
using System.Windows;
using Telerik.Windows.Controls;
namespace CorporateBondCalculator
{
public class RowIsBusyStyleSelector : StyleSelector
{
public Style IsBusyStyle { get; set; }
public Style DefaultStyle { get; set; }
public override Style SelectStyle(object item, DependencyObject container)
{
if (item is MultiBondBackingStore.MultiBondItem)
{
MultiBondBackingStore.MultiBondItem multiBondItem = item as MultiBondBackingStore.MultiBondItem;
if (multiBondItem.IsBusy)
{
return IsBusyStyle;
}
else
{
return DefaultStyle;
}
}
return null;
}
}
}
---------------------------------------------------------
--Resource definition
---------------------------------------------------------
<
UserControl.Resources
>
<
ResourceDictionary
>
<
ResourceDictionary.MergedDictionaries
>
<
ResourceDictionary
Source
=
"/CorporateBondCalculator;component/CBCResources.xaml"
/>
<
ResourceDictionary
Source
=
"/CorporateBondCalculator;component/MultiBond/MultiBondResource.xaml"
/>
</
ResourceDictionary.MergedDictionaries
>
<
uc1:RowIsBusyStyleSelector
x:Key
=
"rowIsBusyStyleSelector"
>
<
uc1:RowIsBusyStyleSelector.IsBusyStyle
>
<
Style
TargetType
=
"telerik:GridViewRow"
>
<
Setter
Property
=
"Foreground"
Value
=
"Gray"
/>
<
Setter
Property
=
"Background"
Value
=
"Gray"
/>
<
Setter
Property
=
"IsEnabled"
Value
=
"False"
/>
</
Style
>
</
uc1:RowIsBusyStyleSelector.IsBusyStyle
>
<
uc1:RowIsBusyStyleSelector.DefaultStyle
>
<
Style
TargetType
=
"telerik:GridViewRow"
>
<
Setter
Property
=
"Foreground"
Value
=
"White"
/>
<
Setter
Property
=
"IsEnabled"
Value
=
"True"
/>
</
Style
>
</
uc1:RowIsBusyStyleSelector.DefaultStyle
>
</
uc1:RowIsBusyStyleSelector
>
</
ResourceDictionary
>
</
UserControl.Resources
>
---------------------------------------------------------
--Grid Definition
---------------------------------------------------------
<
telerikGV:RadGridView
x:Name
=
"BondGrid"
Grid.Row
=
"2"
CanUserInsertRows
=
"False"
ShowInsertRow
=
"False"
FrozenColumnCount
=
"4"
converter:MultiBondGridContextMenu.IsEnabled
=
"True"
ScrollViewer.HorizontalScrollBarVisibility
=
"Auto"
IsReadOnly
=
"False"
SelectionMode
=
"Extended"
SelectionUnit
=
"Cell"
ClipboardPasteMode
=
"Cells, Default"
Pasted
=
"BondGrid_Pasted"
Pasting
=
"BondGrid_Pasting"
PastingCellClipboardContent
=
"BondGrid_PastingCellClipboardContent"
AutoGenerateColumns
=
"False"
Grid.Column
=
"0"
IsFilteringAllowed
=
"False"
ShowGroupPanel
=
"False"
CanUserReorderColumns
=
"True"
RowDetailsVisibilityMode
=
"Collapsed"
RowDetailsVisibilityChanged
=
"BondGrid_RowDetailsVisibilityChanged"
BeginningEdit
=
"BondGrid_BeginningEdit"
CellEditEnded
=
"BondGrid_CellEditEnded"
ElementExporting
=
"BondGrid_ElementExporting"
ElementExported
=
"BondGrid_ElementExported"
RowEditEnded
=
"BondGrid_RowEditEnded"
RowLoaded
=
"BondGrid_RowLoaded"
EnableColumnVirtualization
=
"True"
EnableRowVirtualization
=
"True"
RowStyleSelector
=
"{StaticResource rowIsBusyStyleSelector}"
>
Can you post your data item/IsBusy code?
All the best,Vlad
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
public class MultiBondItem : INotifyPropertyChanged, IDisposable
{
private int isBusy;
pub
lic event PropertyChangedEventHandler PropertyChanged;
--Since there are multiple cells that could be edited, we "count" busy items to determine if the row is busy
public bool IsBusy
{
get
{
return isBusy != 0;
}
set
{
bool wasBusy = isBusy != 0;
if (value == true)
isBusy++;
else
isBusy--;
if (wasBusy != (isBusy != 0))
{
RaisePropertyChanged("IsBusy");
}
}
}
void RaisePropertyChanged(string propertyName, object sender = null)
{
if (sender == null)
sender = BackingStore.Trigger.UI;
if (PropertyChanged != null)
{
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}
}
I've attached an example project for reference.
Greetings,Vlad
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
<ProjectReference Include="..\..\..\..\Work\Development\Controls\GridView\GridView\GridView-SL.csproj">
<Project>{435A1141-A324-422E-8548-749440B13F16}</Project>
<Name>GridView-SL</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\..\Work\Development\Controls\Input\Input_SL.csproj">
<Project>{8A31E9E3-3166-4F5C-BE42-B86115F18AAC}</Project>
<Name>Input_SL</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\..\Work\Development\Core\Controls\Controls_SL.csproj">
<Project>{7EE7B7BA-AB69-40AA-BC04-6C3528A1770B}</Project>
<Name>Controls_SL</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\..\Work\Development\Core\Data\Data_SL.csproj">