Creating DataGrid Templated Column Dynamically - Part II

zhaozj2021-02-16  63

Introductionin

PREVIOUS PART OF This ARTICLE WE SAW How to Use LoadTemplate Method to Dynamically Add Templated Column To The DataGrid. in This Part We will see how to do what using itemplate interface.

Itemplate InterfaceTHIS Interface Found in System.Web.ui Namespace Has One Method with Following Signature.

Void Instantiatein (Control Container);

This Method Must Be Implement In Order To Decide 'Parent' of the template.

Implementing Itemplate Interfacelet 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 will be data bound we add OndataBinding event handler that populates the control with the appropriate values. Then we add this literalcontrol to the container's controls collection. In the OnDataBinding event handler the NamingContainer gives the current DataGridItem (since our parent control is DataGrid) .Adding a template column to the DataGridNow, let us use our implementation of ITemplate interface to add a .

DataGrid DataGrid1 = New DataGrid ();

TemplateColumn TC1 = New TemplateColumn ();

Tc1.ItemTemplate = new ctemplateColumn ("Lastname");

Tc1.Headertext = "Last Name";

DataGrid1.column.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 ();

DA.FILL (DS, "Employees");

DataGrid1.datasource = DS;

DataGrid1.DataMember = "EMPLOYEES";

DataGrid1.databind ();

Here, we have create instance of DataGrid class. We have crerated instance of TemplateColumn class. Our intention is to add a templated column whose ItemTemplate is as decided by our class. Hence we set ItemTemplate property. In the same manner you 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 you should get a DataGrid with single templated Column Titled 'Last Name'.

SummaryIn 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 see this method though a bit complex Gives More Overall Control on the process.

About the Author

Name: Bipin Joshi Email: webmaster@dotnetbips.com Profile:.. Bipin Joshi - the creator and owner of DotNetBips - is a Microsoft MVP, Software Developer and Author He has written dozens of articles for DotNetBips and other web sites Know more about him And dotNetBips here.

转载请注明原文地址:https://www.9cbs.com/read-24229.html

New Post(0)