Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

How to automatically generate columns with predefined settings (MVVM)

$
0
0

Update:
In version 13.2, we significantly improved Data Annotation attribute support for our controls. You can use standard Data Annotation attributes as well as custom ones provided with our MVVM Framework to specify editors and other settings for grid columns. Take a look at  the Data Annotation Attributes and Applying Data Annotations articles to learn more.


If the grid's AutoPopulateColumns option is turned on, the grid automatically generates columns for all fields in a data source. In this instance, cell editors are automatically created based on the type of column values. For instance, if a column is bound to a field that contains DateTime values, the grid creates a date editor for it. If a column is bound to a field that contains numeric data, the numeric editor is used. Otherwise, the text editor is used. Default editors are created dynamically when requested and always have the default behavior.


This example shows how to automatically generate columns with predefined settings.
Column settings are specified via templates defined within Grid Control's resources:

[XML]
<UserControl.Resources><dxg:ColumnTemplateSelectorx:Key="TemplateSelector"/><Stylex:Key="ColumnStyle"TargetType="dxg:GridColumn"><SetterProperty="MinWidth"Value="100"/></Style></UserControl.Resources><dxg:GridControlx:Name="grid"AutoPopulateColumns="True"ColumnGeneratorStyle="{StaticResource ColumnStyle}"ColumnGeneratorTemplateSelector="{StaticResource ResourceKey=TemplateSelector}"ItemsSource="{Binding Source}"><dxg:GridControl.Resources><DataTemplatex:Key="CityTemplate"><ContentControl><dxg:GridColumnx:Name="c1"Visible="True"><dxg:GridColumn.EditSettings><dxe:ComboBoxEditSettingsItemsSource="{Binding Control.DataContext.Cities}"/></dxg:GridColumn.EditSettings></dxg:GridColumn></ContentControl></DataTemplate> ...

In a model that describes a data item, use ColumnGeneratorTemplateNameAttribute to specify which column template is used to present the corresponding data field:

[C#]
[ColumnGeneratorTemplateNameAttribute("CityTemplate")]publicstringCity{get;set;}

As a result, when the Grid Control creates a column, the ColumnTemplateSelector object specified via the grid's ColumnGeneratorTemplateSelector property returns the predefined column template based on ColumnGeneratorTemplateNameAttribute applied to the data field. If this attribute is not applied, the grid creates a column with default settings.

Starting with version 2013 vol 1 the GridControl.AutoPopulateColumns property is marked as obsolete. The GridControl.AutoGenerateColumns property should be used instead.

To learn more on how to implement similar functionality in Silverlight, refer to the T246737 example.


Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>