Creating DataGrid templated Columns Dynamically-part II (transferred from Dotnettips)

Source: Internet
Author: User
Tags constructor
Datagrid

Creating DataGrid templated Columns Dynamically-part II

Introduction

In previous part of this article we saw you loadtemplate method to dynamically add templated columns to the Datagri D. In this part we'll do so using ITemplate interface.

ITemplate Interface

This interface found in System.Web.UI namespace has one method with following signature.
void InstantiateIn (Control container);
This method must is implemented in order to decide ' parent ' of the template.

Implementing ITemplate Interface

Let-us start by creating our own implementation of ITemplate. Create a new class and add following code to it:
Using system;using system.web.ui;using system.web.ui.webcontrols;using system.data;namespace Dynamicdatagridtemplates{public class Ctemplatecolumn:itemplate{    private string colname;     public Ctemplatecolumn (string cname)     {         colname=cname;    }    //must Implement Following method    public void InstantiateIn (Control container)     {         literalcontrol L = new LiteralControl ();         l.databinding +=         new EventHandler ( This. ondatabinding);         container. Controls.Add (l);     }    public void OnDataBinding (object sender, EventArgs e)     {        literalcontrol L = (LiteralControl) sender;         DataGridItem container =          (DataGridItem) L.NamingContainer;         l.text =          ( (DataRowView)         container. DataItem) [ColName]. ToString ();     }}}
Here, the constructor accepts the "column name to which" we want to bind our templated column. We have created a literal control in the InstantiateIn method. Since Our column'll be data bound we add OnDataBinding event handler this populates the control with the appropriate Val UEs. Then we add this literalcontrol to the container ' s Controls collection. In the OnDataBinding event handler the NamingContainer gives the current DataGridItem (since we parent control is Datagri D).

Adding a template column to the DataGrid

Now, let us use our implementation of ITemplate interface to add a templated column to the DataGrid. Add following code in the Page_Load event.
 DataGrid datagrid1=new DataGrid (); TemplateColumn tc1=new TemplateColumn (); Tc1. Itemtemplate=new Ctemplatecolumn ("LastName"); Tc1. headertext= "Last Name";d atagrid1. Columns.Add (TC1); PAGE.CONTROLS[1]. Controls.Add (DATAGRID1); string connstr = @ "Integrated SECURITY=SSPI; User id=sa;initial catalog=northwind;data source=myserver\netsdk "; SqlConnection cnn=new SqlConnection (ConnStr); SqlDataAdapter Da=new SqlDataAdapter ("SELECT * From Employees", CNN) DataSet Ds=new DataSet ();d A. Fill (ds, "Employees");d Atagrid1. DataSource = Ds;datagrid1. DataMember = "Employees";d atagrid1. DataBind (); 
Here, we have create instance of the DataGrid class. We have crerated instance of TemplateColumn class. Our intention are to add a templated column whose ItemTemplate are as decided by our class. Hence we set ItemTemplate property. In the same manner can also set EditItemTemplate. We have passed the column name (LastName) in the constructor of this class. We then add this column to the DataGrid and DataGrid to the page. Binding of DataGrid follows as usual.
After running your application your should get a DataGrid with single templated column titled ' Last Name '.

Summary

In this article we saw how to add a templated column to a DataGrid on the fly using ITemplate interface. We created a custom class that implemented this interface We then set this class as ItemTemplate for the DataGrid. As you'll be though a bit complex gives more overall the process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.