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

Issue with column groups

1 Answer 195 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 21 Aug 2012, 11:43 AM
I am using the RADGridView for the manipulation of data, the data has a number of "week" blocks which consist of 3 columns, i have been using columngroups to create a header for each week block.  The number of week blocks, and therefore columns in the grid, is variable so a dynamic object is used at the item source and the columns generated in the "AutoGeneratingColumn" event, as are the column groups.  The issue is that not all the column groups are being displayed once the grid has been generated, i have checked the properties of the group to check each column has its correct column group and it does.

See the code below (it's a cut down version of the actual code but it produces the same result)

<Window x:Class="ColumnGroupsExample.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="dtMainResource">
            <TextBlock Text="{Binding Path=MainResourceType}" />
        </DataTemplate>
        <DataTemplate x:Key="dtAdjustedCost">
            <TextBlock Text="{Binding Path=AdjustedCost, StringFormat=C}" HorizontalAlignment="Right" Padding="0,0,4,0" />
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <telerik:RadGridView Grid.Row="0"
                            CanUserDeleteRows="False" 
                            CanUserInsertRows="False" 
                            CanUserSortColumns="False"
                            ShowGroupPanel="False"
                            AutoGeneratingColumn="RadGridView_AutoGeneratingColumn"
                            ItemsSource="{Binding Rows}"
                            RowIndicatorVisibility="Collapsed"  
                            AlternateRowBackground="White"
                            AlternationCount="2"
                            Background="AliceBlue"
                            EditTriggers="None"
                            HorizontalGridLinesBrush="SlateGray"
                            SelectionMode="Single"
                            SelectionUnit="FullRow"
                            ShowColumnHeaders="True"
                            IsFilteringAllowed="False"
                            VerticalGridLinesBrush="Transparent"
                             FrozenColumnCount="1">
            <telerik:RadGridView.ColumnGroups>
                <telerik:GridViewColumnGroup Name="Type" Header="Type" />
                <telerik:GridViewColumnGroup Name="Monthly" Header="Monthly" />
            </telerik:RadGridView.ColumnGroups>
        </telerik:RadGridView>
    </Grid>
</Window>

Code behind

 

namespace ColumnGroupsExample
{
    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 Telerik.Windows.Data;
    using Telerik.Windows.Controls;
  
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ViewModel _viewModel;
  
        public MainWindow()
        {
            InitializeComponent();
  
            _viewModel = new ViewModel();
            this.DataContext = _viewModel;
        }
  
        private void RadGridView_AutoGeneratingColumn(object sender, Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs e)
        {
            if (e.Column.UniqueName.Contains("Week"))
            {
                if (e.Column.UniqueName.EndsWith("CostPlan"))
                {
                    DataTemplate costPlanDT = new DataTemplate();
  
                    FrameworkElementFactory spCostPlan = new FrameworkElementFactory(typeof(DockPanel));
                    spCostPlan.SetValue(DockPanel.LastChildFillProperty, false);
  
                    FrameworkElementFactory costPlanEntry = new FrameworkElementFactory(typeof(TextBox));
  
                    costPlanEntry.SetBinding(TextBox.TextProperty, new Binding(e.Column.UniqueName)
                    {
                        NotifyOnSourceUpdated = true,
                        UpdateSourceTrigger = UpdateSourceTrigger.LostFocus,
                        StringFormat = "F2"
                    });
  
                    costPlanEntry.SetValue(TextBox.FontSizeProperty, 10.0);
                    costPlanEntry.SetValue(TextBox.HorizontalContentAlignmentProperty, HorizontalAlignment.Right);
  
                    costPlanEntry.SetValue(TextBox.WidthProperty, 80D);
                    spCostPlan.AppendChild(costPlanEntry);
  
                    costPlanDT.VisualTree = spCostPlan;
                    costPlanDT.Seal();
  
                    e.Column.Header = "Cost Plan";
                    e.Column.CellTemplate = costPlanDT;
                    e.Column.Width = 88;
  
                    int weekNumber = System.Convert.ToInt32(e.Column.UniqueName.Replace("Week", "").Replace("CostPlan", ""));
  
                    ((RadGridView)sender).ColumnGroups.Add(new GridViewColumnGroup()
                    {
                        Name = e.Column.UniqueName.Replace("CostPlan", ""),
                        Header = string.Format("Week : {0} - {1}",
                                                weekNumber,
                                                DateTime.Now.AddDays((weekNumber - 1) * 7).ToString("dd/MM/yyyy"))
                    });
  
                    e.Column.ColumnGroupName = e.Column.UniqueName.Replace("CostPlan", "");
                }
                else if (e.Column.UniqueName.EndsWith("AssessedCost"))
                {
                    DataTemplate assessedCostDT = new DataTemplate();
  
                    FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(DockPanel));
                    spFactory.SetValue(DockPanel.LastChildFillProperty, false);
  
                    FrameworkElementFactory assessedCostsEntry = new FrameworkElementFactory(typeof(TextBox));
  
                    assessedCostsEntry.SetBinding(TextBox.TextProperty, new Binding(e.Column.UniqueName)
                    {
                        NotifyOnSourceUpdated = true,
                        UpdateSourceTrigger = UpdateSourceTrigger.LostFocus,
                        StringFormat = "F2"
                    });
  
                    assessedCostsEntry.SetValue(TextBox.FontSizeProperty, 10.0);
                    assessedCostsEntry.SetValue(TextBox.HorizontalContentAlignmentProperty, HorizontalAlignment.Right);
                    assessedCostsEntry.SetValue(TextBox.WidthProperty, 80D);
  
                    spFactory.AppendChild(assessedCostsEntry);
  
                    assessedCostDT.VisualTree = spFactory;
                    assessedCostDT.Seal();
  
                    e.Column.Header = "Assessed Cost";
  
                    e.Column.CellTemplate = assessedCostDT;
                    e.Column.Width = 88;
                    e.Column.ColumnGroupName = e.Column.UniqueName.Replace("AssessedCost", "");
                }
                else if (e.Column.UniqueName.EndsWith("Difference"))
                {
                    DataTemplate differenceDt = new DataTemplate();
  
                    FrameworkElementFactory difference = new FrameworkElementFactory(typeof(TextBlock));
  
                    difference.SetBinding(TextBlock.TextProperty, new Binding(e.Column.UniqueName)
                    {
                        StringFormat = "C"
                    });
  
                    difference.SetValue(TextBlock.FontSizeProperty, 10.0);
                    difference.SetValue(TextBlock.PaddingProperty, new Thickness(0, 0, 4, 0));
  
                    difference.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Right);
  
                    difference.SetValue(TextBlock.WidthProperty, 80D);
  
                    differenceDt.VisualTree = difference;
                    differenceDt.Seal();
  
                    e.Column.Header = "Difference";
  
                    e.Column.CellTemplate = differenceDt;
                    e.Column.ColumnGroupName = e.Column.UniqueName.Replace("Difference", "");
  
                }
                else if (e.Column.UniqueName.EndsWith("CF03DetailDateUid"))
                {
                    //Don't show this column
                    e.Column.IsVisible = false;
                }
            }
            else
            {
                if (e.Column.UniqueName == "MainResourceType")
                {
                    e.Column.CellTemplate = (DataTemplate)this.Resources["dtMainResource"];
                    e.Column.Header = "Resource Type";
                    e.Column.ColumnGroupName = "Type";
                }
                else if (e.Column.UniqueName == "Projection")
                {
                    DataTemplate projectionDT = new DataTemplate();
  
                    FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(DockPanel));
                    spFactory.SetValue(DockPanel.LastChildFillProperty, false);
  
                    FrameworkElementFactory projectionEntry = new FrameworkElementFactory(typeof(TextBox));
  
                    projectionEntry.SetBinding(TextBox.TextProperty, new Binding(e.Column.UniqueName)
                    {
                        NotifyOnSourceUpdated = true,
                        UpdateSourceTrigger = UpdateSourceTrigger.LostFocus,
                        StringFormat = "F2"
                    });
  
                    projectionEntry.SetValue(TextBox.FontSizeProperty, 10.0); ;
                    projectionEntry.SetValue(TextBox.StyleProperty, (Style)this.Resources["detailEntry"]);
                    projectionEntry.SetValue(TextBox.HorizontalContentAlignmentProperty, HorizontalAlignment.Right);
                    projectionEntry.SetValue(TextBox.WidthProperty, 80D);
  
                    spFactory.AppendChild(projectionEntry);
  
                    projectionDT.VisualTree = spFactory;
                    projectionDT.Seal();
                    e.Column.CellTemplate = projectionDT;
  
                    e.Column.Header = "Projection";
                    e.Column.ColumnGroupName = "Monthly";
                }
                else if (e.Column.UniqueName == "AdjustedCost")
                {
                    //#FFCCFFFF
                    e.Column.CellTemplate = (DataTemplate)this.Resources["dtAdjustedCost"];
                    e.Column.Header = "Adjusted Costs";
                    e.Column.ColumnGroupName = "Monthly";
                }
            }
        }
    }
}

View Model

namespace ColumnGroupsExample
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Dynamic;
    using System.Collections.ObjectModel;
    using System.Windows.Input;
  
    public class ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private ObservableCollection<dynamic> _rows;
  
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
  
        public ViewModel()
        {
            _rows = new ObservableCollection<dynamic>();
  
            CreateRow("Staff");
            CreateRow("Labour");
            CreateRow("Plant");
            CreateRow("Temp Materials");
            CreateRow("Perm Materials");
            CreateRow("Sub Contract");
        }
  
        public void CreateRow(string type)
        {
            var row = new ExpandoObject() as IDictionary<string, Object>;
  
            decimal zero = 0;
            Guid? blankUid = Guid.Empty;
  
            row.Add("MainResourceType", type);
            row.Add("AdjustedCost", zero);
            row.Add("Projection", zero);
  
            for (int i = 1; i <= 13; i++)
            {
                row.Add(string.Format("Week{0}CostPlan", i), zero);
                row.Add(string.Format("Week{0}AssessedCost", i), zero);
                row.Add(string.Format("Week{0}Difference", i), zero);
                row.Add(string.Format("Week{0}CF03DetailDateUid", i), blankUid);
            }
  
            _rows.Add(row);
        }
  
        public ObservableCollection<dynamic> Rows
        {
            get
            {
                return _rows;
            }
            set
            {
                if (_rows != value)
                {
                    _rows = value;
                    OnPropertyChanged("Rows");
                }
            }
  
        }
    }
}

 

 

 


Please advise on how to get the column groups displaying correctly.

Cheers

Steve


1 Answer, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 24 Aug 2012, 01:50 PM
Hello Steve,

I believe the very next internal build will have a fix relevant to the issue you are experiencing. it will be available for download in your account the late Monday or Tuesday.

Please let me know if upgrading to the fresh dlls does not fix the problem .

Kind regards,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Steve
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Share this question
or