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

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

Hierarchical GridView in ASP.NET with AJAX, JQuery implementation - Part 1

Previously I had blogged some post related to showing two grids in hierarchical way. For example - when I need to show an invoice reports on the screen, the user requesting to show the list of invoices at initial time and they can drill down the details of item details of each invoice by clicking on respective row. The implementations can be in multiple ways, but I had blogged the implementation with AJAX and without AJAX using Java script on ASP.NET page. Below are the URLs of the same – Hierarchical GridView in ASP.NET Hierarchical GridView in ASP.NET with AJAX, Javascript implementation In this post I am taking the same requirements done before and enhance with some additional functionality as per readers expectations. The requirement on this implementation will be – The page should show a Grid View with list of Orders with Expand icon on the first column. On click of expand icon on each row, the Order Details of the Order must be fetched from the database ...

Scrollable Gridview With fixheader using JQuery in Asp.net

Scrollable Gridview With fixheader using JQuery in Asp.net Introduction: In this article I will explain how to implement scrollable gridview with fixed header in asp.net using JQuery.  Description:  In Previous posts I explained lot of articles regarding Gridview. Now I will explain how to implement scrollable gridview with fixed header in asp.net. I have one gridview that contains lot of records and I used  paging for gridview  but the requirement is to display all the records without paging. I removed paging at that time gridview occupied lot of space because it contains more records to solve this problem we implemented scrollbar.  After scrollbar implementation if we scroll the gridview we are unable to see Gridview header.   To implement Scrollable gridview with fixed header I tried to implement concept with css and JavaScript but there is no luck because maintaining fixed header working in IE but not in Mozilla and vice versa to ...