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

CellStyleSelector on auto generated columns

6 Answers 376 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Gama
Top achievements
Rank 1
Gama asked on 03 Feb 2012, 12:29 AM
I'm trying to add a CellStyleSelector on autogenerated columns, but no style is being applied.  The grid is below.

<telerik:RadGridView Name="rgridProgramSearchResults" AutoGenerateColumns="True" AutoGeneratingColumn="rgridProgramSearchResults_AutoGeneratingColumn" CanUserFreezeColumns="False" IsReadOnly="True" Grid.Column="1"
                ItemsSource="{Binding}" RowIndicatorVisibility="Collapsed" ShowGroupPanel="False" Margin="0,0,0,30" SelectionMode="Multiple" >
    <telerik:RadGridView.Columns>
        <telerik:GridViewSelectColumn UniqueName="Select" />
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

and the style
<me:ImportStyle x:Key="stadiumCapacityStyle">
    <me:ImportStyle.BigStadiumStyle>
        <Style TargetType="telerik:GridViewCell">
            <Setter Property="Background" Value="Red"/>
        </Style>
    </me:ImportStyle.BigStadiumStyle>
    <me:ImportStyle.SmallStadiumStyle>
        <Style TargetType="telerik:GridViewCell">
            <Setter Property="Background" Value="Yellow" />
        </Style>
    </me:ImportStyle.SmallStadiumStyle>
</me:ImportStyle>

The auto generate event
private void rgridProgramSearchResults_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
{
     e.Column.CellStyleSelector = Application.Current.Resources["stadiumCapacityStyle"] as StyleSelector;
}


and the class

public class ImportStyle : StyleSelector
{
    public override Style SelectStyle(object item, DependencyObject container)
    {
        return BigStadiumStyle;
 
    }
    public Style BigStadiumStyle { get; set; }
    public Style SmallStadiumStyle { get; set; }
}

6 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 03 Feb 2012, 12:00 PM
Hi,

 I have tested how the CellStyleSelector is applied to the column inside the AutoGeneratingColumn event and it was applied fine. May you please confirm what is the version that you use? Is the behaviour the same with the latest version?

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Gama
Top achievements
Rank 1
answered on 06 Feb 2012, 06:12 PM
I created a small project below.  As far as I understand, all the columns shoudl be red but no styling is being done.

MainWindow.xaml
        x:Class="columns.MainWindow"
         xmlns:me="clr-namespace:columns"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
 
            <me:ImportStyle x:Key="stadiumCapacityStyle">
                <me:ImportStyle.BigStadiumStyle>
                    <Style TargetType="telerik:GridViewCell">
                        <Setter Property="Background" Value="Red"/>
                    </Style>
                </me:ImportStyle.BigStadiumStyle>
            </me:ImportStyle>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <telerik:RadGridView Name="rgridProgramSearchResults" AutoGenerateColumns="True"  CanUserFreezeColumns="False" IsReadOnly="True" Grid.Column="1" AutoGeneratingColumn="rgridProgramSearchResults_AutoGeneratingColumn"
                        ItemsSource="{Binding}" RowIndicatorVisibility="Collapsed" ShowGroupPanel="False" >
        </telerik:RadGridView>
 
    </Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
 
namespace columns
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataTable dt = new DataTable();
 
            dt.Columns.Add("Col1");
            dt.Columns.Add("Col2");
            dt.Columns.Add("Col3");
 
            DataRow dr = dt.NewRow();
 
            dr["Col1"] = "row1Col1";
            dr["Col2"] = "row1Col2";
            dr["Col3"] = "row1Col3";
 
            dt.Rows.Add(dr);
            dr = dt.NewRow();
 
            dr["Col1"] = "row2Col1";
            dr["Col2"] = "row2Col2";
            dr["Col3"] = "row2Col3";
 
            dt.Rows.Add(dr);
            dr = dt.NewRow();
 
            dr["Col1"] = "row3Col1";
            dr["Col2"] = "row3Col2";
            dr["Col3"] = "row3Col3";
            dt.Rows.Add(dr);
            rgridProgramSearchResults.ItemsSource = dt;
        }
 
        private void rgridProgramSearchResults_AutoGeneratingColumn(object sender, Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs e)
        {
            e.Column.CellStyleSelector = Application.Current.Resources["stadiumCapacityStyle"] as StyleSelector;
        }
    }
    public class ImportStyle : StyleSelector
    {
        public override Style SelectStyle(object item, DependencyObject container)
        {
            return BigStadiumStyle;
        }
        public Style BigStadiumStyle { get; set; }
    }
}
0
Dimitrina
Telerik team
answered on 07 Feb 2012, 11:32 AM
Hi,

 As I can see from your code snippet you have defined the "stadiumCapacityStyle" inside Grid.Resources, then in the code you try to access it from the Application resources, not from the current Grid's resources.

I have attached a sample project showing how the correct style is applied.

Regards,
Didie
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Gama
Top achievements
Rank 1
answered on 07 Feb 2012, 08:59 PM
Yep, that did it.  Thanks
0
Eliot
Top achievements
Rank 1
answered on 27 Jan 2015, 12:43 PM
Hi,

This is most of what I was looking for.

It doesn't however show how you can return a style based on the column header.

I am trying to return a weekend and weekday style for the entire column.

The item in the handler is a row.  It doesn't show the columns I am dynamically adding at runtime 



 foreach (DateTime day in EachDay(_startDate, (DateTime)_endDate))
            {

                var dateColumn = new GridViewExpressionColumn() { Header = day.ToString("dd MMM yyyy"), UniqueName = day.ToString("dd MMM yyyy"), DataFormatString = "{0:##,###}", IsReadOnly = true };

                dateColumn.CellStyleSelector = Application.Current.Resources["FundingGridColumnStyle"] as StyleSelector;

                columns.Add(dateColumn);

                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;
            }         

0
Boris
Telerik team
answered on 30 Jan 2015, 11:45 AM
Hi Eliot,

You can easily return a cell style based on the value of its column header. All you need to do is use the container parameter of the SelectStyle() method like so:

public override Style SelectStyle(object item, DependencyObject container)
      {
          if (container is GridViewCell)
          {
              var cell = container as GridViewCell;
              var columnHeader = cell.Column.Header;             
          }
 
          return bigStadium;
      }

I hope this helps.

Regards,
Boris
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.

 
Tags
GridView
Asked by
Gama
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Gama
Top achievements
Rank 1
Eliot
Top achievements
Rank 1
Boris
Telerik team
Share this question
or