Dynamically create a template column for a DataGrid

Source: Internet
Author: User
Tags implement inheritance touch
datagrid| Create | Dynamic Sometimes we need a very complex DataGrid, we know Datagrid,datalist and other controls have template columns, we can through the Dynamic State template column to achieve, complex logic of the state. Because page inherits TemplateControl, you can use the method in the TemplateControl class inside the Page object loadtemplate, We can use this method to load the specified path user control to implement a rich representation (incidentally, there is also a LoadControl method and loadtemplate have the same parameter type, that is, we can use the LoadControl method to dynamically load the user control, Can implement a custom user interface, the page elements into small user controls can be loaded according to the user's definition, we can also implement the ITemplate interface to achieve the dynamic state of the version column.

1, using LoadTemplate to achieve:

Let's look at an example where we build a asp.net Web application, and the user control that adds an ascx is called Webusercontrol1.ascx as follows, and there is only one label control in the user control to state a LastName field:

<%@ control language= "C #"%>

<asp:label id= "Label1" runat= "server" text= ' <%# DataBinder.Eval ' (((DataGridItem) Container). DataItem, "LastName")%> ' ></asp:label>

Next we're going to create a DataGrid control DataGrid1, and we'll add the following code to 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 ();

Before we analyze the above code, we use a sample database Northwind from SQL Server. We'll get all the employee information and then populate the dataset, and then we'll declare a itemplate type of object temp to load the state-defined user control. We're declaring a templatecolumn to dynamically create a template column, and next we add information to the template column, including HeaderText and so on, Because we're going to ItemTemplate. So we assign the temp just loaded to the ItemTemplate object in the template column, and finally we add the new Touch column to the DataGrid and state the data.

Note that the above process, our user control has a data state of the label this is very important, only in this way we can achieve the data state function, or is to display a column with the same information.

2, using ITemplate to achieve:

Above we use LoadTemplate to implement dynamic touch version of the state of the column, and then we will use the ITemplate interface to implement. The ITemplate interface has a method InstantiateIn (Control Container). This method must specify the parent control of the touch version column. The following code implements the ITemplate interface, and 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;

}

In order to use the method that the interface must implement

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

}

}

}

Within the constructor we specify the column name for the State column. We used InstantiateIn to create a LiteralControl control L, and we added event-state events to the control so that we could process the state-defined control when the DataGrid was set up, and in order to implement event-state events, We have also written the event handler function ondatabinding, where we will use the specified data.

Next, we add dynamically to the DataGrid a list of our custom touches, and the following code is 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 new a DataGrid out, and then declare a template column TC1, in the set TC1 ItemTemplate for us to customize a template column (do not forget to use the column name this parameter), and then specify the template column of other information, Finally, use dataset state data (don't forget to add the control to its parent control, for example: DataGrid1.) Columns.Add (TC1);

The above introduces two kinds of dynamic State template version of the method, I hope to help beginners, in fact, the method here is very simple, I think the most important question here is how to understand the object-oriented, hope that through the description of this article beginners can have a better understanding of the object-oriented, We use the inheritance of the interfaces and the relationships between the parent subclasses, by using the inheritance of the interface we can make a template column factory can use the same pattern to produce different template columns, because we are using the interface (see design mode for more information).



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.