Apply DataTemplate for dynamically generate columns r RadGridView

2 posts, 0 answers
  1. Vajda
    Vajda avatar
    2 posts
    Member since:
    Jul 2013

    Posted 15 Nov 2014 Link to this post


    in MVVM scenario I need dynamically generate columns for RadGridView.

    I created attached property for this purpose.

    ItemSource is bind to property ObservableCollection<Company> Companies from VM

    where Company is

    public class Company : INotifyPropertyChanged
            public Company()
                Dept = new Dictionary<string, User>();
            private Dictionary<string,User> dept;
            public Dictionary<string,User> Dept
                get { return dept; }
                    dept = value;

    and User is

    public class User : INotifyPropertyChanged
            private string firstName;
            public string FirstName
                get { return firstName; }
                    firstName = value;
            private string lastName;
            public string LastName
                get { return lastName; }
                    lastName = value;

    I generate columns in VM in this way.

    In VM I have property ObservableCollection<ColumnDefinition> which is bind to attached property in View.

    ColumnDefinition is abstraction for GridViewDataColumn.

    <telerik:RadGridView ItemsSource="{Binding Path=Country.Companies, UpdateSourceTrigger=PropertyChanged}"
                         dynamicCol:RadGridViewColumnsBinding.ColumnsCollection="{Binding Path=Columns}">
            <DataTemplate x:Key="UserTemplate"
                          DataType="{x:Type models:User}">
                    <TextBlock Text="LastName:" />
                    <TextBlock Text="{Binding Path=LastName}" />

    When I need add column to RadGridView I add ColumnDefinition obj to property Columns.

    I need apply DataTemplate on GridViewDataColumn and this is which not work for me.

    Exactly binding to property User.LastName in DataTemplate not work.

    I use this binding exp for GridViewDataColumn 

    string bindingExp = string.Format("Dept[{0}]", columnName);
    //string bindingExp = string.Format("Dept[{0}].LastName", columnName); // whithout datatemplate this work
                          var binding = new Binding(bindingExp)
                              UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                          Columns.Add(new ColumnDefinition
                              Header = columnName,
                              DataMemberBinding = binding,
                              Tag = ColumnTag,
                              UniqueName = Guid.NewGuid().ToString(),
                              CellTemplateName = "UserTemplate",

    I try attached several times sample project but always get error:

    The selected file(s) cannot be attached because it may exceed the maximum attachment size (2 MB) or is from not allowed type (allowed: .gif, .jpg, .jpeg, .png).

    Attached file size is 74 KB.

    Hence I uploded sample project to onedrive.

    You can download here.
  2. Dimitrina
    Dimitrina avatar
    3769 posts

    Posted 17 Nov 2014 Link to this post


    In order to have predefined columns, I would suggest you to use a Behavior for setting RadGridView.Columns collection. You can check the "Binding Columns From ViewModel" sdk example on how to do so. Although GitHub is a very well-known platform we saw a better and easier approach for reviewing our examples by developing our SDK Samples Browser.

    I hope this helps.


    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.

Back to Top