Dynamically create a DataGrid template Column

Source: Internet
Author: User

Sometimes we need to stick a complicated DataGrid. We know that the DataGrid, datalist, and other controls all have template columns. We can achieve this through dynamic bonding of template columns and complex logic. Because page inherits templatecontrol, you can use the method loadtemplate in the templatecontrol class in the page object, we can use this method to load the user control of the specified path to implement rich representation (by the way, there is also a loadcontrol method with the same parameter type as loadtemplate, that is to say, we can use the loadcontrol method to dynamically load user controls, implement custom user interfaces, and divide page elements into small user controls that can be loaded according to user definitions ), we can also implement the itemplate interface to implement dynamic partitioning of touch columns.

1. Use loadtemplate to implement:

Let's look at an example. We create an ASP. net web application. After adding an ascx user control called webusercontrol1.ascx, there is only one label control in the user control to specify a lastname field:

<% @ Control Language = "C #" %>

<Asp: Label id = "label1" runat = "server" text = '<% # databinder. eval (datagriditem) container ). dataitem, "lastname") %> '> </ASP: Label>

Next we will create a DataGrid Control datagrid1. We will add the following code in the page_load event:

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 ");

Itemplate temp = page. loadtemplate ("webusercontrol1.ascx ");

Templatecolumn Tc = new templatecolumn ();

TC. headertext = "last name ";

TC. itemtemplate = temp;

Datagrid1.columns. Add (TC );

Datagrid1.datasource = Ds;

Datagrid1.datamember = "employees ";

Datagrid1.databind ();

First, let's analyze the above Code. We use the example database northwind that comes with SQL Server. We will get all employee information, then fill in the dataset, and then declare an itemplate type object temp to load the federated user control. We declare a templatecolumn to dynamically create a template column. Next we will add information to this template column, including headertext, as itemtemplate is about to be bound, we assign the loaded Temp value to the itemtemplate object of the template column. Finally, we add the new touch column to the DataGrid and bond the data.

Note that in the above process, it is very important to have a data-bonded label in our user control. Only in this way can we implement the data-Bonded Function. Otherwise, a column with the same information is displayed.

2. Implement using itemplate:

In the above example, we use loadtemplate to implement the dynamic pattern columns. Next we will use the itemplate interface. The itemplate interface has a method instantiatein (control container ). This method must specify the parent control of the touch column. The following code implements the itemplate interface. We use the following code to create a new class:

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;

}

// Methods required to use interfaces

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 ();

}

}

}

In the constructor, we specify a column name for the bunding column. We used instantiatein to create a literalcontrol control L. At the same time, we added event bonding events for this control, so that we can handle this control when binding the DataGrid, at the same time, we have also compiled the event processing function ondatabinding to implement event bonding. Here we will use the specified column to bond data.

Next, we will dynamically add our custom touch version columns to the DataGrid. The following code is from page_load:

DataGrid datagrid1 = new DataGrid ();

Templatecolumn TC1 = new templatecolumn ();

Tc1.itemtemplate = new ctemplatecolumn ("lastname ");

Tc1.headertext = "last name ";

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

Da. Fill (DS, "employees ");

Datagrid1.datasource = Ds;

Datagrid1.datamember = "employees ";

Datagrid1.databind ();

First, we will create a new DataGrid and declare a template column TC1. In the itemtemplate setting of Tc1, We will customize a template column (do not forget to use the column name parameter ), specify other information about the template column and use dataset to specify data (do not forget to add the control to its parent control, for example, datagrid1.columns. add (TC1 );).

The above describes two methods for dynamically binding template columns, hoping to help beginners. In fact, the method here is very simple. I think the most important question here is how to understand object-oriented, I hope that the introduction in this article will allow beginners to have a better understanding of object-oriented objects. Here we use the inheritance of interfaces and the relationship between parent class subclasses, using the inheritance of interfaces, we can create a template column. factories can use the same mode to generate different template columns, because we use interfaces (for details, see design patterns).

 

 

This article references the http://www.dotnetbips.com website

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.