Export (0) Print
Expand All

Creating Templates Programmatically in the DataGrid Control

Visual Studio .NET 2003

A DataGrid control can contain template columns in which you lay out the column by adding controls and literal text. Template columns provide you with substantially more flexibility than do bound columns, button columns, and hyperlink columns. As with templates for the DataList and Repeater controls, you can dynamically create templates for the DataGrid control. This allows you to define the contents, layout, and data of a column at run time.

Note   The procedures for working with a dynamic template column are similar to working with a dynamic template in a Repeater or DataList control. For general information on creating dynamic templates, see Creating Web Server Control Templates Dynamically.

The following are the differences between using a dynamic template in a DataGrid control and in the Repeater or DataList controls:

  • You do not create item templates for the grid itself; instead, you create them for a column in the grid.
  • There are slightly different templates for a DataGrid column than for a Repeater or DataList control. A DataGrid column does not include an alternating item or separator template. However, like the DataList control, it does include an EditItem template.

To create a dynamic template column

  1. Create a template class that implements the ITemplate interface of the System.Web.UI namespace. For details, see Creating Web Server Control Templates Dynamically.
  2. Optionally, pass into the class's constructor a value that the class can use to determine what type of template to create (ItemTemplate, EditItemTemplate, and so on).

    Typically, the template type is passed using a value from the ListItemType enumeration already defined in the DataGrid control.

  3. In the class, implement the InstantiateIn method (the only member of the ITemplate interface). This method provides a way to insert an instance of text and controls into the specified container.

    The following example shows a template class for a dynamic template column. The constructor accepts two parameters: the first parameter specifies the template type to create and the second parameter allows you to pass in the name of the column you are creating. Because this is a template for a DataGrid control, the class includes code to create an EditItem template containing a Textbox control.

    ' Visual Basic
    Private Class DataGridTemplate
       Implements ITemplate
       Dim templateType As ListItemType
       Dim columnName As String
    
       Sub New(ByVal type As ListItemType, ByVal ColName As String)
          templateType = type
          columnName = ColName
       End Sub
    
       Sub InstantiateIn(ByVal container As Control) _
          Implements ITemplate.InstantiateIn
          Dim lc As New Literal()
          Select Case templateType
             Case ListItemType.Header
                lc.Text = "<B>" & columnName & "</B>"
                container.Controls.Add(lc)
             Case ListItemType.Item
                lc.Text = "Item " & columnName
                container.Controls.Add(lc)
             Case ListItemType.EditItem
                Dim tb As New TextBox()
                tb.Text = ""
                container.Controls.Add(tb)
             Case ListItemType.Footer
                lc.Text = "<I>Footer</I>"
                container.Controls.Add(lc)
          End Select
       End Sub
    End Class
    
    // C#
    public class DataGridTemplate : ITemplate
    {
       ListItemType templateType;
       string columnName;
       
       public DataGridTemplate(ListItemType type, string colname)
       {
          templateType = type;
          columnName = colname;
       }
    
       public void InstantiateIn(System.Web.UI.Control container)
       {
          Literal lc = new Literal();
          switch(templateType)
          {
             case ListItemType.Header:
                lc.Text = "<B>" + columnName + "</B>";
                container.Controls.Add(lc);
                break;
             case ListItemType.Item:
                lc.Text = "Item " + columnName;
                container.Controls.Add(lc);
                break;
             case ListItemType.EditItem:
                TextBox tb = new TextBox();
                tb.Text = "";
                container.Controls.Add(tb);
                break;
             case ListItemType.Footer:
                lc.Text = "<I>" + columnName + "</I>";
                container.Controls.Add(lc);
                break;
          }
       }
    }
    

After you have created the class for a dynamic template column, you can use it to assign columns to the DataGrid control at run time.

To use dynamic template columns

  1. Create an instance of the TemplateColumn class.
  2. Create an instance of your dynamic template, passing it an item type value if appropriate.
  3. Assign the instance to one of the template properties of the TemplateColumn object you created in Step 1, such as ItemTemplate, EditItemTemplate, HeaderTemplate, and so on.

    The following example shows how to use the dynamic template column to add two columns to the DataGrid control. In this example, the templates are instantiated during the page load and before the control is bound to its data source.

    ' Visual Basic
    Private Sub Page_Load(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles MyBase.Load
       Dim tc1 As New TemplateColumn()
       tc1.HeaderTemplate = New _
          DataGridTemplate(ListItemType.Header, "Column1")
       tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, _
          "Column1")
       tc1.EditItemTemplate = New _
          DataGridTemplate(ListItemType.EditItem, "Column1")
       tc1.FooterTemplate = New _
          DataGridTemplate(ListItemType.Footer, "Column1")
       DataGrid1.Columns.Add(tc1)
       
       Dim tc2 As New TemplateColumn()
       tc2.HeaderTemplate = New _
          DataGridTemplate(ListItemType.Header, "Column2")
       tc2.ItemTemplate = New _
          DataGridTemplate(ListItemType.Item, "Column2")
       tc2.EditItemTemplate = New _
          DataGridTemplate(ListItemType.EditItem, "Column2")
       tc2.FooterTemplate = New _
          DataGridTemplate(ListItemType.Footer, "Column2")
       DataGrid1.Columns.Add(tc2)
       SqlDataAdapter1.Fill(DsCategories1)
       DataGrid1.DataBind()
    End Sub
    
    // C#
    private void Page_Load(object sender, System.EventArgs e)
    {
       TemplateColumn tc1 = new TemplateColumn();
       tc1.HeaderTemplate = new 
          DataGridTemplate(ListItemType.Header, "Column1");
       tc1.ItemTemplate = new 
          DataGridTemplate(ListItemType.Item, "Column1");
       tc1.EditItemTemplate = new 
          DataGridTemplate(ListItemType.EditItem, "Column1");
       tc1.FooterTemplate = new 
          DataGridTemplate(ListItemType.Footer, "Column1");
       DataGrid1.Columns.Add(tc1);
    
       TemplateColumn tc2 = new TemplateColumn();
       tc2.ItemTemplate = new 
          DataGridTemplate(ListItemType.Item, "Column2");
       tc2.HeaderTemplate = new 
          DataGridTemplate(ListItemType.Header, "Column2");
       tc2.EditItemTemplate = new 
          DataGridTemplate(ListItemType.EditItem, "Column2");
       tc2.FooterTemplate = new 
          DataGridTemplate(ListItemType.Footer, "Column2");
       DataGrid1.Columns.Add(tc2);
       sqlDataAdapter1.Fill(dsCategories1);
       DataGrid1.DataBind();
    }
    

See Also

Web Server Controls Templates | Creating Web Server Control Templates Dynamically

Show:
© 2014 Microsoft