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...

Editing Child GridView in Nested GridView

Editing Child GridView in Nested GridView In this article we will explore how to edit child gridview in the nested gridview.   Let''s write some code. Step 1:  Add scriptmanager in the aspx page. < asp : ScriptManager   ID ="ScriptManager1"   runat ="server"   EnablePageMethods ="true"> </ asp : ScriptManager > Step 2:  Add below stylesheet for modal popup. < style   type ="text/css">        .modalBackground        {              background-color : Gray;              filter : alpha(opacity=80);              opacity : 0.5;       }        .ModalWindow        {     ...

Add Edit Update Records in GridView using Modal Popup in ASP.Net

Add Edit Update Records in GridView using Modal Popup in ASP.Net In this article, I’ll explain how to Add and Edit records in ASP.Net GridView control using ASP.Net AJAX Control Toolkit Modal Popup Extender. Database For this tutorial, I am using Microsoft’s NorthWind database. You can download it using the following link. Download Northwind Database Connection string Below is the connection string to connect to the database. < connectionStrings >     < add name = " conString "      connectionString = " Data Source=.\SQLExpress;database=Northwind;     Integrated Security=true " /> </ connectionStrings >   HTML Markup Below is the HTML Markup of the page. Below you will notice that I have placed a Script Manager and an ASP.Net Update Panel on the page. Inside the Update Panel I have placed an ASP.Net GridView Control along with a Modal Popup Extender that will be used to Add or...