Defining Columns

RadGridView provides two mechanisms for defining its columns:

This help article explains how to do both with code examples.

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 the sample below.

Example 1: RadGridView generates columns automatically.

Example 1: Defining the business object

public class Employee 
    [DisplayAttribute(AutoGenerateField = false)] 
    public int EmployeeId 
    [DisplayAttribute(Name = "First Name")] 
    public string FirstName 
    [DisplayAttribute(Name = "Last Name")] 
    public string LastName 
Public Class Employee 
    Public Property EmployeeId() As Integer 
            Return m_EmployeeId 
        End Get 
        Set(value As Integer) 
            m_EmployeeId = value 
        End Set 
    End Property 
    Private m_EmployeeId As Integer 
    <DisplayAttribute(Name:="First Name")> 
    Public Property FirstName() As String 
            Return m_FirstName 
        End Get 
        Set(value As String) 
            m_FirstName = value 
        End Set 
    End Property 
    Private m_FirstName As String 
    <DisplayAttribute(Name:="Last Name")> 
    Public Property LastName() As String 
            Return m_LastName 
        End Get 
        Set(value As String) 
            m_LastName = value 
        End Set 
    End Property 
    Private m_LastName As String 
End Class 

Figure 1: The result of letting RadGridView automatically generate two columns.

Telerik WPF DataGrid defining columns 1

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:

You must add the columns to the Columns collection of RadGridView. Later on in the application lifecycle, you can easily access them via the UniqueName property of GridViewColumn, or by the index of the column. To see this sequence unfold, look at the following step-by-step example.

  • Define a column of type GridViewDataColumn.

Example 2: 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 3: Defining a GridViewDataColumn programmatically

GridViewDataColumn column = new GridViewDataColumn(); 
Dim column As New GridViewDataColumn() 
  • Set Header property and add the column to the Columns collection. The default Header will be the property specified as DataMemberBinding.

You can utilize the UniqueName property of a column only when you have also configured the DataMemberBinding for it. Otherwise, please define GridViewColumn and use its Header property instead of the UniqueName one.

Example 4: Defining a column declaratively with UniqueName property set

<telerik:RadGridView x:Name="radGridView" 
        <telerik:GridViewDataColumn DataMemberBinding="{Binding FirstName}" Header="First Name" UniqueName="FirstName" /> 

Example 5: Defining a column programmatically with UniqueName property set

GridViewDataColumn column = new GridViewDataColumn(); 
column.DataMemberBinding = new Binding("FirstName"); 
column.Header = "My Column"; 
column.UniqueName = "MyColumn"; 
this.radGridView.AutoGenerateColumns = false; 
Dim column As New GridViewDataColumn() 
column.DataMemberBinding = New Binding("FirstName") 
column.Header = "My Column" 
column.UniqueName = "MyColumn" 
Me.radGridView.AutoGenerateColumns = False 
  • To access the column later, use the UniqueName value or its column index as a key for the Columns collection.

Example 6: Accessing a column by index

var myColumn = this.radGridView.Columns["MyColumn"]; 
myColumn = this.radGridView.Columns[0]; 
Dim myColumn = Me.radGridView.Columns("MyColumn") 
myColumn = Me.radGridView.Columns(0) 
  • In order to set a CellTemplate of GridViewDataColumn in code-behind you can store a defined DataTemplate within Resources collection as follows:

Example 7: Defining a DataTemplate

    <DataTemplate x:Key="dataTemplate1"> 
        <TextBlock Text="Text"/> 
Then reference it in code-behind as demonstrated below:

Example 8: Applying the defined DataTemplate through the CellTemplate property

GridViewDataColumn column = new GridViewDataColumn(); 
column.CellTemplate = (DataTemplate)this.Resources["dataTemplate1"]; 
Dim column As New GridViewDataColumn() 
column.CellTemplate = CType(Me.Resources("dataTemplate1"), DataTemplate) 

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.

Example 9: Copying the properties of one column to another

GridViewDataColumn col = new GridViewDataColumn(); 
Dim col As GridViewDataColumn = New GridViewDataColumn() 

See Also

In this article
Not finding the help you need? Improve this article