Skip to main content

Runtime Dynamically Creating Bound and Template Columns in GridView using Asp.net C#


In some complex scenarios developers need to create runtime GridView dynamically. So obviously developers need to create dynamic columns for dynamic gridviews. Here in this article I will explain how one can develop or implement runtime dynamically create bound column as well as template column of a GridView control and also how to bind data into the dynamically created GridView. For simplicity here i use a datatable but you can bind data from database as well. Here I also showed how developers can write dynamic event handler for dynamically created button within the template column. The output will be:
Dynamic GridView

Creating bound column is easier than template column because if you want to add dynamic template column in your GridView then you must implement ITemplate interface. When you instantiate the implemented object then it will automatically call the "InstantiateIn" method. To implement my example first add a class in your project and named it "TemplateHandler". Then copy the code sample:
01using System;
02using System.Web.UI;
03using System.Web.UI.WebControls;
04 
05public class TemplateHandler : ITemplate
06{
07    void ITemplate.InstantiateIn(Control container)
08    {
09        Button  cmd= new Button();
10        cmd.ID = "cmd";
11        cmd.Text = "HI";
12        cmd.Click += new EventHandler(Dynamic_Method);
13        container.Controls.Add(cmd);
14    }
15 
16    protected void Dynamic_Method(object sender, EventArgs e)
17    {
18        ((Button)sender).Text = "Hellooooo";
19    }
20}

Now add a page in your project & copy the below codes under page_load event:
01protected void Page_Load(object sender, EventArgs e)
02    {
03        DataTable dt = new DataTable();
04 
05        dt.Columns.Add("FirstName");
06        dt.Columns.Add("LastName");
07        dt.Columns.Add("Age", typeof(System.Int32));
08 
09        DataRow oItem = dt.NewRow();
10        oItem[0] = "Shawpnendu";
11        oItem[1] = "Bikash";
12        oItem[2] = 32;
13        dt.Rows.Add(oItem);
14 
15        oItem = dt.NewRow();
16        oItem[0] = "Bimalendu";
17        oItem[1] = "Bikash";
18        oItem[2] = 27;
19        dt.Rows.Add(oItem);
20 
21 
22        GridView gv = new GridView();
23        gv.AutoGenerateColumns = false;
24 
25        BoundField nameColumn = new BoundField();
26        nameColumn.DataField = "FirstName";
27        nameColumn.HeaderText = "First Name";
28        gv.Columns.Add(nameColumn);
29 
30        nameColumn = new BoundField();
31        nameColumn.DataField = "LastName";
32        nameColumn.HeaderText = "Last Name";
33        gv.Columns.Add(nameColumn);
34 
35        nameColumn = new BoundField();
36        nameColumn.DataField = "Age";
37        nameColumn.HeaderText = "Age";
38        gv.Columns.Add(nameColumn);
39 
40        // Here is template column portion
41        TemplateField TmpCol = new TemplateField();
42        TmpCol.HeaderText = "Click Me";
43        gv.Columns.Add(TmpCol);
44        TmpCol.ItemTemplate = new TemplateHandler();       
45 
46        gv.DataSource = dt;
47        gv.DataBind();
48 
49        Form.Controls.Add(gv);
50    }

Comments

Popular posts from this blog

JQuery lightbox image slideshow gallery in asp.net

Introduction: In this article I will explain how to create lightbox image slideshow in asp.net using JQuery. Description:  In previous post I explained about  Ajax SlideshowExtender sample  to display images slideshow. Now in this article I will explain how to create lightbox image slideshow in asp.net using JQuery. In many websites generally we will see different types of slideshows like whenever we click on image that is automatically open with lightbox effect and we have a chance to see all the remaining images by press next or previous options in slideshow. All these Slideshows are implemented by using JQuery plugins. After seen those slideshows I decided to write post to use JQuery plugins to implement beautiful slideshown in asp.net.  To implement this one I am using  previous post  insert and display images from folder based on imagepath in database  because in that post I explained clearly how to insert images into our project folder a...

Scrollable GridView with Fixed Headers using jQuery Plugin

Using the same example I have created a jQuery Plugin for Scrollable GridView with Fixed header so that you can directly make a GridView scrollable.   HTML Markup < form   id ="form1"   runat ="server"> < asp : GridView   ID ="GridView1"   runat ="server"   AutoGenerateColumns   =   "false"> < Columns > < asp : BoundField   DataField   =   "ContactName"   HeaderText   =   "Contact Name"   /> < asp : BoundField   DataField   =   "City"   HeaderText   =   "City"   /> < asp : BoundField   DataField   =   "Country"   HeaderText   =   "Country"   /> Columns > asp : GridView > form >   Applying the Scrollable Grid jQuery Plugin < script   src ="Scripts/jquery-1.4.1.min.js"   type ="text/javascript"> script > < script   src ="Scripts/Scro...

Generics

Generics allow us to have type or method which can operate on objects of various types, while providing type safety at the compile time. That means if you define generics class object of type Integer & you are trying to add different type of variable to generic class like String then it will give compile time error. In another sense, if we try to add different types in same list like string and int in a same list then compile time error will be thrown. Generics are available under the namespace System. Collections .Generic. The concept of type parameters is introduce to the .NET Framework with the help of Generics, which makes it possible to design classes & methods which defer the specification of one or more types until that class or method is declared and instantiated by the client code. public class MyGenericClass { void Add(T val) { } ...