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

[Solved] How to create columns programmatically

3 Answers 321 Views
Grid for XAML
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Lampros
Top achievements
Rank 1
Lampros asked on 13 Oct 2014, 10:22 PM
Hello,

I'm using the RadDataGrid with a collection of columns that are bound to the data source using something like:

<telerikGrid:RadDataGrid GridLinesVisibility="Horizontal" Grid.Column="1" Name="dataGrid" AutoGenerateColumns="False" UserFilterMode="Enabled" UserEditMode="None" UserSortMode="Single">
<telerikGrid:RadDataGrid.Columns>
<telerikGrid:DataGridTextColumn PropertyName="Property1" Header="Header1"/>
<telerikGrid:DataGridTextColumn PropertyName="Property2" Header="Header2"/>
</telerikGrid:RadDataGrid.Columns>
            </telerikGrid:RadDataGrid>
 
My items source is a collection of objects that have the following properties:
string Property1
string Property2
KeyValuePair<string, string> Property3

Is it possible to dynamically generate columns based on the key values of Property3 and bind them to the corresponding values of the same property?         

3 Answers, 1 is accepted

Sort by
0
Tsvyatko
Telerik team
answered on 14 Oct 2014, 08:40 AM
Hello Stergios,

If you like to use nested properties, you can use TemplatedColumn instead of TextColumn. Keep in mind that Bindings will not work with structures (as KeyValuePair). So we can suggest one of the following:

 - Expose nested property value through other property and use TextColumn:

public string Value
{
    get
    {
        return this.Property3.Value;
    }
}


 - Change Property3 type to class (e.g. Tuple) and use TemplatedColumn:

public Tuple<string, string> Property3 { get; set; }
<telerikGrid:DataGridTemplateColumn Header="Value">
                  <telerikGrid:DataGridTemplateColumn.CellContentTemplate>
                      <DataTemplate>
                          <TextBlock Text="{Binding Property3.Item2}" VerticalAlignment="Center"/>
                      </DataTemplate>
                  </telerikGrid:DataGridTemplateColumn.CellContentTemplate>
              </telerikGrid:DataGridTemplateColumn>


Let us know if this helps.

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

 
0
Lampros
Top achievements
Rank 1
answered on 17 Oct 2014, 03:59 PM
Thank you for your support.

I understand your approach (I'm not sure though how "Binding Property3.Item2" works) but in your example the template column is hardcoded in XAML.
In my scenario I want to generate a series of data grid columns programmatically for each key-value pair or Tuple<string,string>. The pairs are coming from an external data source and the number of columns is not a priory known.

In the WPF version of the telerik data grid I was using something like the following example:

var genericPartDataColumnsQry = from key in genericPartDataKeys
select new GridViewDataColumn()
{
Name = key,
Header = IETM_WPF.Resources.Resource.ResourceManager.GetString(key, IETM_WPF.Resources.Resource.Culture) ?? key,
DataMemberBinding = new Binding("") { Converter = GenericPartDataConverter.Instance, ConverterParameter = key }
};

gridView.Columns.AddRange(genericPartDataColumnsQry);

0
Tsvyatko
Telerik team
answered on 21 Oct 2014, 09:42 AM
Hi Stergios,

In your scenario I would suggest to utilize the use of DynamicObject which DataGrid supports. I have prepared simple project demonstrating how you can the desired scenario.

Regards,
Tsvyatko
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
Grid for XAML
Asked by
Lampros
Top achievements
Rank 1
Answers by
Tsvyatko
Telerik team
Lampros
Top achievements
Rank 1
Share this question
or