It's possible to set the background color of a cell (cell, not column or row) based on a particular value??
Yesterday I spent 2 hours figuring out but I had no luck... I tried on the RowLoaded event... maybe I should do it by javascript??
Thanks in advance
Paolo
8 Answers, 1 is accepted
The only way I can think of that will work in Silverlight is something like this:
static void OnRowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e) |
{ |
Message message = e.DataElement as Message; |
if (message != null) |
{ |
Binding binding = new Binding("Size"); |
binding.Source = message; |
binding.Mode = BindingMode.OneWay; |
binding.Converter = new SizeToBackgroundConverter(); |
e.Row.Cells[2].SetBinding(Control.BackgroundProperty, binding); |
} |
} |
I have attached the sample project that this code snippet is from. Inside it you can find the business object class Message that implements INotifyPropertyChanged and the converter used to decide what color to return based on the value. I hope this helps.
Sincerely yours,
Ross
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
private void rgvTimes_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e) |
{ |
if (e.Row is Telerik.Windows.Controls.GridView.GridViewRow) |
if (e.DataElement != null) |
{ |
ProxyCode.Polyrol_Time pt = e.DataElement as ProxyCode.Polyrol_Time; |
//------------------------------------------------------------------------------------------------------------- |
// If the EndTime of the Time grid is null we set the background to green and if it's not null we set it to red |
//------------------------------------------------------------------------------------------------------------- |
e.Row.Cells[5].Background = new SolidColorBrush((pt.EndTime == null ? Colors.Green : Colors.Red)); |
} |
} |
Hope this helps...
I am have done an upgrade to 2011 Q1 from the 2010 Q3 issue ,on previous issue i was setting the backgroud color of a cell and it was working fine , now on the new 2011 issue the colouring of the background (cell) has become inactive. (in this case i cannot use a converter in xaml as i need more than one parameter).
Here is a snap of my code.
private void syncGrid_CellLoaded ( object sender, Telerik.Windows.Controls.GridView.CellEventArgs e )
{Data.
ThresholdDetail threshold;
Data.Sync row = (Data.Sync)e.Cell.ParentRow.Item;
if ( row != null )
{
switch ( e.Cell.Column.UniqueName )
{
case "MFP" :
threshold = dataContext.Thresholds
.Where ( R => R.hiId ==
"MFP")
.Where ( R => R.acqTypeName == row.ACQTYPE )
.Where ( R => R.AcqId == row.ACQID )
.FirstOrDefault ();
if ( threshold != null && row.MFP > threshold.daSteadyThrValue )
{
e.Cell.Background = (
SolidColorBrush)Application.Current.Resources["ThresholdExceedenceColor"];
}
{
e.Cell.Background =
new SolidColorBrush ( Colors.Transparent );
}
break ;
........
Xaml
......
<controls:GridView Name="syncGrid"
Grid.Row="0"
ItemsSource="{Binding Path=Sync}"
SelectedItem="{Binding Path=SelectedSync, Mode=TwoWay}"
AutoGenerateColumns="False"
IsReadOnly="True"
CellLoaded="syncGrid_CellLoaded">
.......
The event is fired when the grid is triggerd/loaded but this has no effect on the grid's cell , (2010 Q3 was fine)
Thank you for your help
Kind Regards
Giuseppe
Thank you for reporting the issue.
We will log it as a bug and we will try to address it in some of the next internal builds.
Please excuse us for any inconvenience caused.
Kind regards,
Yordanka
the Telerik team
I have also tried to use RowLoaded event.
The problem is that only non visible cells are treated only when user moves scrollbars.
I mean when the grid is displayed the visibile columns are treated , if there are other columns on the right
but in that moment not visible, outside the browser page, all the cell conditons/code is lost.
Awaiting new version.
Kind Regards,
Giuseppe
I have tried some other ways to understand better the behaviour posted.
As far as i can see if CellLoaded or RowLoaded is fired inside the Xaml code where i try
to chage the background of the cell, this does not work on those cells that are rendered
on the first page,. Meanwhile if you use the scrollbar then you will have a result on those cell's
that are re rendered on the page
Otherwise it seems that if you fire the event after that the Grid has been rendered
(doing this from the code-behind) all the cell's will be colored.
In fact if you use the example source code posted from telerik (in this page)
you can notice this effect.
Hope that it is more clear and awiating for the next isssue where i hope it will be resolved.
Kind Regards,
Giuseppe
Thank you for the details!
In the meantime, you can use CellStyleSelector to achieve your goals. Here is an online example demonstrating this approach.
Kind regards,
Yordanka
the Telerik team