Edit this page

Defining Columns

You can define columns for RadGridView in two ways:

  • let RadGridView automatically generate the columns based on the underlying data source.
  • manually specify columns in XAML or code-behind.

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 the RadGridView control to a collection of employees (see code in Example 1 and the result in Figure 1), RadGridView will create a separate column for each public propertiy of the Employee object.

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.

C#

public class Employee
{
    [DisplayAttribute( AutoGenerateField = false )]
    public int EmployeeId
    {
        get;
        set;
    }
    [DisplayAttribute( Name = "First Name" )]
    public string FirstName
    {
        get;
        set;
    }
    [DisplayAttribute( Name = "Last Name" )]
    public string LastName
    {
        get;
        set;
    }
}

VB

Public Class Employee
    <DisplayAttribute(AutoGenerateField:=False)> _
    Public Property EmployeeId() As Integer
        Get
            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
        Get
            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
        Get
            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.

Rad Grid View definingcolumns 1

Manual Columns Definition

Using the built-in auto generation of columns does not fit all scenarios. In such cases you can manually define columns you want to be displayed. When defining a column you can choose between several column types:

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

  • Define a column of type GridViewDataColumn.

XAML

<telerik:GridViewDataColumn/>

You will find the column types in the same namespace (Telerik.Windows.Controls) and assembly (Telerik.Windows.Controls.GridView.dll) as RadGridView.

C#

GridViewDataColumn column = new GridViewDataColumn();

VB

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 set and work with the UniqueName of a column only when you have also set the DataMemberBinding. Otherwise, please use the Header instead of UniqueName.

XAML

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

C#

GridViewDataColumn column = new GridViewDataColumn();
column.DataMemberBinding = new Binding( "FirstName" );
column.Header = "My Column";
column.UniqueName = "MyColumn";
this.radGridView.AutoGenerateColumns = false;
this.radGridView.Columns.Add( column );

VB

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

C#

GridViewDataColumn myColumn = this.radGridView.Columns[ "FirstColumn" ];
myColumn = this.radGridView.Columns[ 0 ];

VB

Dim myColumn As GridViewDataColumn = 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:

XAML

<Window.Resources>
    <DataTemplate x:Key="dataTemplate1">
        <TextBlock Text="Text"/>
     </DataTemplate>
</Window.Resources>

Then reference it in code-behind as demonstrated below:

C#

GridViewDataColumn column = new GridViewDataColumn();
column.CellTemplate = (DataTemplate)this.Resources["dataTemplate1"];
radGridView.Columns.Add(column );

VB

Dim column As New GridViewDataColumn()
column.CellTemplate = DirectCast(Me.Resources("dataTemplate1"), DataTemplate)
Me.radGridView.Columns.Add(column)

See Also