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

Customizing with GridTemplateColumn

There are cases in which you may want to have two level headers or custom complex structure for your table cells. This appearance is attainable using GridTemplateColumns with HTML tables in the HeaderTemplate/ItemTemplate. Here is an example which holds two tables inside those templates:

Customizing the HeaderTemplate

<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" AllowSorting="True" AutoGenerateColumns="false">
      <telerik:GridBoundColumn HeaderText="ContactName" DataField="ContactName" UniqueName="ContactName">
      <telerik:GridTemplateColumn UniqueName="TemplateColumn">
          <table id="Table1" cellspacing="1" cellpadding="1" width="300" border="1">
              <td colspan="2" align="center">
              <td width="50%">
              <td width="50%">
                <b>Postal code</b>
          <table id="Table2" cellspacing="1" cellpadding="1" width="300" border="1">
              <td width="50%">
                <%# DataBinder.Eval(Container.DataItem, "City") %>
              <td width="50%">
                <%# DataBinder.Eval(Container.DataItem, "PostalCode") %>

Handle the NeedDataSource event in the code behind:

private void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    OleDbConnection MyOleDbConnection = new OleDbConnection(("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/Nwind.mdb")));
    OleDbDataAdapter MyOleDbDataAdapter = new OleDbDataAdapter();
    MyOleDbDataAdapter.SelectCommand = new OleDbCommand("SELECT TOP 5 ContactName, Address, City, PostalCode FROM Customers", MyOleDbConnection);
    DataTable myDataTable = new DataTable();
    RadGrid1.DataSource = myDataTable.DefaultView;
Private Sub RadGrid1_NeedDataSource(ByVal [source] As System.Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
    Dim MyOleDbConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/Nwind.mdb"))
    Dim MyOleDbDataAdapter As New OleDbDataAdapter
    MyOleDbDataAdapter.SelectCommand = New OleDbCommand("SELECT TOP 5 ContactName, Address, City, PostalCode FROM Customers", MyOleDbConnection)
    Dim myDataTable As New DataTable
    End Try

    RadGrid1.DataSource = myDataTable.DefaultView
End Sub

You may also refer to the following online example of Telerik RadGrid which demonstrates the usage of GridTemplateColumn.

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