New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Working with Autogenerated Columns

In cases where you want to have a column for each field in the grid datasource and you have no need for specific column declarations, or when the number and type of columns is going to change dynamically, you could let the grid auto-generate its columns by setting AutoGenerateColumns = "true" (default value)in the control declaration.

This article will explain the main specifics of the feature, as well as how you can customize these columns.

In order to enable both Grouping and Column Aggregates for the autogenerated columns, set the UseAllDataFields property of the MasterTableView to true

Auto-generated columns specifics

When you set the AutoGenerateColumns property of RadGrid to true, the RadGrid control generates a separate column for each data field in the control's assigned datasource. Based on the datatype of the field, the control will generate:

  • GridBoundColumn for string fields

  • GridCheckBoxColumn for boolean fields

  • GridDateTimeColumn for datetime fields

  • GridNumericColumn for numeric fields

The columns will be added to the existing ones (if any) in the Columns collection of the MasterTableView (or current GridTableView in a hierarchy). If you declare any other columns, they will be rendered first, before the auto-generated ones.

The HeaderText of the columns will be generated by splitting the name of the DataField of the column by upper-case characters. For example, if a data field is called "FirstName", the column header text will be "First Name". If you want to prevent the text from splitting, set RadGrid.MasterTableView.EnableSplitHeaderText=false. In case you want to provide a custom header text, you could set it explicitly for the column as shown in the next section of this article.

If you want to also add columns for edit and delete operations without declaring them in mark-up or code, you can set respectively the AutoGenerateEditColumn and AutoGenerateDeleteColumn properties of RadGrid to true. This will add a GridEditCommandColumn and a GridButtonColumn with a Delete command name to the Columns collection.

Customizing auto-generated columns

When you want to customize the auto-generated columns of RadGrid, you could use the OnColumnCreated server event. It fires for each auto-generated column in RadGrid (including structure columns). You can get the column from the event arguments and check its data type or unique name to recognize it. In this event you could set the column properties and customize the look and behavior of the column.

protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
{
    if (e.Column.UniqueName == "FirstName")
    {
        e.Column.HeaderText = "Name";
        e.Column.AutoPostBackOnFilter = true;
    }
}
Protected Sub RadGrid1_ColumnCreated(ByVal sender As Object, ByVal e As GridColumnCreatedEventArgs)
    If e.Column.UniqueName = "FirstName" Then
        e.Column.HeaderText = "Name"
        e.Column.AutoPostBackOnFilter = True
    End If
End Sub

If you need to customize the cells of a given column you could do it just the same way as with regular columns (check out the "See Also" section below). You should only note that the * UniqueName of an auto-generated column is the same as the name of the data field it is bound to *.

In case the number of fields and the names in the grid datasource are dynamic and you do not know the UniqueNames of the columns, you could loop through the AutoGeneratedColumns collection of the current GridTableView and pick the UniqueName of each column fulfilling a given condition and use it to access a cell in the current row. The code snippet below shows how to customize the font of all columns bound to a string field:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
        {
            if (col.DataType == typeof(int))
            {
                item[col.UniqueName].Font.Bold = true;
            }
        }
    }
}
Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
        For Each col As GridColumn In RadGrid1.MasterTableView.AutoGeneratedColumns
            If col.DataType Is GetType(Integer) Then
                item(col.UniqueName).Font.Bold = True
            End If
        Next
    End If
End Sub

See Also

In this article