Defining Columns

You can define columns for RadGridView in two ways. First, you can let RadGridView automatically generate the columns based on the underlying data source. Second, you can describe columns manually in the XAML or code-behind. This help article will describe how to do both with code examples in the two sections below.

Automatic Columns Generation

By default, the RadGridView control 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 created a separate column for each one of the public properties of your Employee object.

Example 1: RadGridView can automatically generate columns.

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 of using RadGridView. In such cases you have to manually define columns for the fields you want to be displayed in your grid. When defining a column you are able to choose between several column types - GridViewColumn, GridViewDataColumn, GridViewComboBoxColumn, GridViewHyperLinkColumn, GridViewImageColumn, GridViewSelectColumn, etc. You must add the defined columns to the Columns collection of the 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.

  1. 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 the RadGridView control.

C#

GridViewDataColumn column = new GridViewDataColumn();

VB

Dim column As New GridViewDataColumn()
  1. Set the Header and the UniqueName properties and add the column to the Columns collection of the RadGridView.

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" UniqueName="FirstName" />
    </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)
  1. 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)
  1. In order to set a CellTemplate of GridViewDataColumn in code-behind you can store a defined DataTemplate within Window's ResourceCollection as follows:

XAML

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

And 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