Dynamically create a DataGrid template column

xiaoxiao2021-03-20  234

Sometimes we need a complicated DataGrid, we know that DataGrid, DataList and other controls have a template column, we can implement, complex logic's bonding through dynamic bonding template columns. Since Page inherits TemplateControl, you can use the method loadingTemplate in the templateControl class in the Page object. We can use this method to load the specified path user control to achieve a rich representation (by way of making a loadControl method and loadTemplate has the same Parameter type, that is, we can use the loadControl method to dynamically load user controls, you can implement custom user interfaces, divide the page elements into small user controls can be loaded according to the user's definition), we can also implement the Itemplate interface implementation Dynamic bonding of the column.

1, use the loadTemplate implementation:

Let's take an example, we create an ASP.NET web application, adding a ASCX user control called WebUserControl1.ascx as follows, only one label control is used in the user control to Bonds a LastName field:

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

2, use Itemplate implementation:

Above We use the LoadTemplate to implement the dynamic tuning column, then we will use the Itemplate interface to implement. The itemplate interface has a method instantiatein (Control Container). This method must specify the father control of the tap column. The following code will implement 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;

}

/ / In order to use the interface must be implemented

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

}

}

}

Inside the constructor we specify a column name for the Bangjun. We created a LiteralControl control L using Instantiatein, and we add events to this control, so we can handle this control in Bang DataGrid, and we have written events to implement event bonding events. Processing the function onDatabase, where we will use the specified list.

Next, we add our custom japonic columns to DataGrid, as follows: 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 first discharged a DataGrid, then declare a template column TC1, set the TC1 ItemTemplate for us to customize a template column (don't forget this parameter of the column name), then specify additional information of this template column, and finally take advantage of DataSet Data (don't forget to add controls to its parent control, such as DataGrid1.columns.Add (TC1);).

The above method introduced two ways of dynamic bonding model, I hope that the beginners can help, in fact, the method here is very simple, I think the most critical problem here is how to understand the object-oriented, I hope to pass this article. Description Beginner can have a better understanding of the object, we use the relationship between the interface inheritance and the parent subclass. We can make a model of the same model by using the inheritance of the interface. The template column, because we use the interface (see "Design Mode" in detail).

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

New Post(0)