New to Telerik UI for WPF? Download free 30-day trial

Defining Columns

RadGridView provides two mechanisms for defining its columns:

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

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 

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

private void gridView_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)  
    if((e.Column as GridViewDataColumn).DataMemberBinding.Path.Path == "Name") 
        var newColumn = new GridViewDataColumn(); 
        newColumn.Header = "Full Name"; 
        newColumn.Width = 100; 
        e.Column = newColumn; 
Private Sub gridView_AutoGeneratingColumn(ByVal sender As Object, ByVal e As GridViewAutoGeneratingColumnEventArgs) 
    If (TryCast(e.Column, GridViewDataColumn)).DataMemberBinding.Path.Path = "Name" Then 
        Dim newColumn = New GridViewDataColumn() 
        newColumn.Header = "Full Name" 
        newColumn.Width = 100 
        e.Column = newColumn 
    End If 
End Sub 

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 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

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

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

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

Example 6: Defining a column programmatically with UniqueName property set

GridViewDataColumn column = new GridViewDataColumn(); 
column.DataMemberBinding = new Binding("FirstName"); 
column.Header = "My Column"; 
this.radGridView.AutoGenerateColumns = false; 
Dim column As New GridViewDataColumn() 
column.DataMemberBinding = New Binding("FirstName") 
column.Header = "My Column" 
Me.radGridView.AutoGenerateColumns = False 

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

var myColumn = this.radGridView.Columns["MyColumn"]; 
myColumn = this.radGridView.Columns[0]; 
Dim myColumn = Me.radGridView.Columns("MyColumn") 
myColumn = Me.radGridView.Columns(0) 

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 8: Copying the properties of one column to another

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

See Also

In this article