Dynamically Adding Template columns to a GridView

来源:互联网 发布:网络信息采集与编辑 编辑:程序博客网 时间:2024/04/30 19:41


Introduction

Many a times we wonder how to add template column dynamically. This might be the case when number of columns, column type etc. cannot be decided during design time. For example, if the data source of a Gridview control will be retrieved from a Business layer, during designing the Gridview user interface, developer can not determine no. of columns and other things related to column description. In such a case, Gridview will be configured to generate columns automatically at runtime. Adding template column at runtime was not possible in earlier version of .Net i.e. 1.0. With the new version .NET 2.0 onwards, there is a facility to add template column at runtime dynamically.

For the above purpose, I have created a class 'DynamicTemplate' inheriting 'ITemplate' interface.

public class DynamicTemplate : System.Web.UI.ITemplate

Now the very first step is to implement constructor for the class:

public
DynamicTemplate(System.Web.UI.WebControls.ListItemType type)


{

templateType = type;

}

Here I have passed Listitemtype as a parameter to the constructor, which will define the type of current template. i.e. template is Header, Item, AlternatingItem or Footer.

Next, for defining the type of control template column will be displaying, I have added following method:

public void AddControl(WebControl wbControl,String
BindPropertyName, String
BindExpression)


{

htControls.Add(htControls.Count, wbControl);

htBindPropertiesNames.Add(htBindPropertiesNames.Count, BindPropertyName);

htBindExpression.Add(htBindExpression.Count, BindExpression);

}

Here we need to pass 3 parameters, wbcontrol will be reference to any web control which we want our template column to display, BindPropertyName will be the property name of the control to be binded i.e. Text property for a textbox, label control. BindExpression will be the field name or any valid Bind expression to be evaluated and assigned to the BindProperty.

Now for providing template column funtionality, we need to implement 'InstantiateIn' method. This method will be called for every row of gridview before Binding occurs.

Collapse

public void InstantiateIn(System.Web.UI.Control container)


{

PlaceHolder ph = new PlaceHolder();

for (int i = 0; i < htControls.Count; i++)

{

//clone control

Control cntrl = CloneControl((Control)htControls);

switch (templateType)

{

case ListItemType.Header:

break;

case ListItemType.Item:

ph.Controls.Add(cntrl);

break;

case ListItemType.AlternatingItem:

ph.Controls.Add(cntrl);

ph.DataBinding += new EventHandler(Item_DataBinding);

break;

case ListItemType.Footer:

break;

}

}

ph.DataBinding += new EventHandler(Item_DataBinding);

container.Controls.Add(ph);

}

Here we first clone the webcontrol so that a new copy can be created and then we add this new copy to the desired placeholder.

Next, we want to implement this class for adding template column dynamically:

TemplateField t = new TemplateField();


DynamicTemplate mt = new DynamicTemplate(ListItemType.Item);

TextBox t1 = new TextBox();

t1.ID = "txt";

t1.Visible = true;

t1.Text = "1";

mt.AddControl(t1, "Text", "Sno");

Here we are creating a template column which will display a textbox. And text property of this textbox will be binded with the Sno data field of configured data source.

A complete implementation of the above class is attached with this article which is quite self explanatory and simple.
原创粉丝点击