Runtime Dynamically Creating GridView and Bind Data in Bound Columns Using Asp.Net C# VB.Net

In some complex scenarios developers need to create Run time 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 run time dynamically create bound columns of a GridView control and also how to bind data into the dynamically created GridView Bound Columns. For simplicity here i use a datatable but you can bind data from database as well.

Objective/Outcome:
Runtime Dynamically Creating GridView and Bind Data in Bound Columns Using Asp.Net Csharp VB.Net

To place the Dynamic GridView add a Placeholder like Below:

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

Note: The run time dynamically created GridView will be positioned into this PlaceHolder.

Creating GridView in Run Time & Bind Data to Bound Columns:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("FirstName");
            dt.Columns.Add("LastName");
            dt.Columns.Add("Age", typeof(System.Int32));

            DataRow oItem = dt.NewRow();
            oItem[0] = "Shawpnendu";
            oItem[1] = "Bikash";
            oItem[2] = 32;
            dt.Rows.Add(oItem);

            oItem = dt.NewRow();
            oItem[0] = "Bimalendu";
            oItem[1] = "Bikash";
            oItem[2] = 27;
            dt.Rows.Add(oItem);

            oItem = dt.NewRow();
            oItem[0] = "Purnendu";
            oItem[1] = "Bikash";
            oItem[2] = 24;
            dt.Rows.Add(oItem);

            GridView gv = new GridView();
            gv.HeaderStyle.BackColor=System.Drawing.Color.CornflowerBlue;
            gv.HeaderStyle.Font.Bold=true;
            gv.HeaderStyle.ForeColor=System.Drawing.Color.White;
            gv.CellPadding = 5;
            gv.AutoGenerateColumns = false;

            BoundField nameColumn = new BoundField();
            nameColumn.DataField = "FirstName";
            nameColumn.HeaderText = "First Name";
            gv.Columns.Add(nameColumn);

            nameColumn = new BoundField();
            nameColumn.DataField = "LastName";
            nameColumn.HeaderText = "Last Name";
            gv.Columns.Add(nameColumn);

            nameColumn = new BoundField();
            nameColumn.DataField = "Age";
            nameColumn.HeaderText = "Age";
            gv.Columns.Add(nameColumn);

            gv.DataSource = dt;
            gv.DataBind();

            PlaceHolder1.Controls.Add(gv);
        }
    }

VB.Net Code:

   Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim dt As DataTable = New DataTable()

            dt.Columns.Add("FirstName")
            dt.Columns.Add("LastName")
            dt.Columns.Add("Age", System.Type.GetType("System.Int64"))

            Dim oItem As DataRow = dt.NewRow()
            oItem(0) = "Shawpnendu"
            oItem(1) = "Bikash"
            oItem(2) = 32
            dt.Rows.Add(oItem)

            oItem = dt.NewRow()
            oItem(0) = "Bimalendu"
            oItem(1) = "Bikash"
            oItem(2) = 27
            dt.Rows.Add(oItem)

            oItem = dt.NewRow()
            oItem(0) = "Purnendu"
            oItem(1) = "Bikash"
            oItem(2) = 24
            dt.Rows.Add(oItem)

            Dim gv As GridView = New GridView()
            gv.HeaderStyle.BackColor = System.Drawing.Color.CornflowerBlue
            gv.HeaderStyle.Font.Bold = True
            gv.HeaderStyle.ForeColor = System.Drawing.Color.White
            gv.CellPadding = 5
            gv.AutoGenerateColumns = False

            Dim nameColumn As BoundField = New BoundField()
            nameColumn.DataField = "FirstName"
            nameColumn.HeaderText = "First Name"
            gv.Columns.Add(nameColumn)

            nameColumn = New BoundField()
            nameColumn.DataField = "LastName"
            nameColumn.HeaderText = "Last Name"
            gv.Columns.Add(nameColumn)

            nameColumn = New BoundField()
            nameColumn.DataField = "Age"
            nameColumn.HeaderText = "Age"
            gv.Columns.Add(nameColumn)

            gv.DataSource = dt
            gv.DataBind()

            PlaceHolder1.Controls.Add(gv)

        End If
    End Sub

Now run the project. Hope you will get your generated GridView. Happy programming.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, Gridview, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*