Defining Columns
RadGridView provides two mechanisms for defining its columns:
Automatic columns definition based on the underlying data source.
Manual columns definition in XAML or code-behind.
This help article explains how to do both with code examples. You can also copy properties from one column to another.
Automatic Columns Generation
By default, RadGridView will generate its columns automatically based on the underlying data source. When, for example, you set the ItemsSource of RadGridView to a collection of employees (see code in Example 1 and the result in Figure 1), the control will create a separate column for each public property of the Employee object.
Specific editors will be generated for the following types:
- String: TextBox editor (default)
- Boolean: CheckBox editor accompanied by GridViewCheckBox element displayed in view mode
- DateTime: RadDatePicker editor
However, if you wish to explicitly specify the column name for certain property of your data class or to prevent the creation of a column for it, use the System.ComponentModel.DataAnnotations.DisplayAttribute, as it is shown in Example 1.
Example 1: Defining the business object
If you wish to further customize the generated columns, you can handle the AutoGeneratingColumn event as shown in Example 2.
Example 2: Customizing auto-generated columns
Manual Columns Definition
Using the built-in auto generation of columns does not fit all scenarios. In such cases you can manually define the needed columns. When defining a column you can choose between several column types:
- GridViewColumn
- GridViewBoundColumnBase
- Data Column
- Hyperlink Column
- Dynamic Hyperlink Column
- Expression Column
- Image Column
- Select Column
- CheckBox Column
- ComboBox Column
- MultiColumnComboBox Column
- Masked Input Column
- Toggle Row Details Column.
You must add the columns to the Columns collection of RadGridView. Later on in the application lifecycle, you can easily access them via the Columns indexer by specifying the name of the property the column is bound to or the index of the column.
Examples 2 and 3 demonstrate how to define a column both in XAML and code-behind.
Example 3: Defining a GridViewDataColumn declaratively
You will find the column types in the same namespace (Telerik.Windows.Controls) and assembly (Telerik.Windows.Controls.GridView.dll) as RadGridView.
Example 4: Defining a GridViewDataColumn programmatically
You can then set the column's Header property and add the column to the Columns collection. The default Header will be the property specified as DataMemberBinding.
Example 5: Defining a column declaratively with DataMemberBinding property set
Example 6: Defining a column programmatically with UniqueName property set
Similarly, you can set any of the column's other properties or copy them from another column.
To access the column later, use the string used for the DataMemberBinding value or its column index as a key for the Columns collection.
Example 7: Accessing a column by index
Copying Properties from Another Column
RadGridView provides a mechanism for easily fetching the properties of one column to another. This can be done through the CopyPropertiesFrom method of GridViewColumn.