Dynamically create DataTable and bind to GridView in ASP.Net C#,VB

dynamically create DataTable and bind to GridView in ASP.Net C#,VBIntroduction: In this article I am going to share how we can create dynamic datatable, add columns, rows and data in it and bind to gridview.


Description: As we know we can use datatable to bind data from database. But there may be some reasons to dynamically build and bind data to gridview. In that case we can create datatable and add required columns specifying their data type and then we can create rows and add data in those columns.

Implementation: Let’s create a demo page for demonstration purpose.

HTML Source
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
          <asp:GridView ID="grdEmployeeDetails" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
                <AlternatingRowStyle BackColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"></HeaderStyle>
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#F5F7FB" />
                <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                <SortedDescendingCellStyle BackColor="#E9EBEF" />
                <SortedDescendingHeaderStyle BackColor="#4870BE" />
            </asp:GridView>
        </div>
    </form>
</body>
</html>

Asp.Net C# Code to dynamically create datatable and bind to gridview.

using System;
using System.Data;

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridview();
        }
    }
       protected void BindGridview()
    {
        DataTable dt = new DataTable();
        //Add Columns to datatable
        dt.Columns.Add("EmployeeId", typeof(Int32));
        dt.Columns.Add("Employee Name", typeof(string));
        dt.Columns.Add("Employee Code", typeof(string));
        //Adding rows and data
        dt.Rows.Add(1, "Ajay", "EMP0001");
        dt.Rows.Add(2, "Irfan", " EMP0002");
        dt.Rows.Add(3, "Hrithik", " EMP0003");
        dt.Rows.Add(4, "Ranbir", " EMP0004");
        dt.Rows.Add(5, "Salman", " EMP0005");
        dt.Rows.Add(6, "Aamir", " EMP0006");      
        dt.Rows.Add(7, "Akshay", " EMP0007");
        //Bind datatable to gridview
        grdEmployeeDetails.DataSource = dt;
        grdEmployeeDetails.DataBind();
    } 

Asp.Net VB Code to dynamically create datatable and bind to gridview.
Imports System
Imports System.Data

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindGridview()
        End If
    End Sub
        Protected Sub BindGridview()
        Dim dt As New DataTable()
        'Add Columns to datatable
        dt.Columns.Add("EmployeeId", GetType(Int32))
        dt.Columns.Add("Employee Name", GetType(String))
        dt.Columns.Add("Employee Code", GetType(string))
        'Adding rows and data
        dt.Rows.Add(1, "Ajay", " EMP0001")
        dt.Rows.Add(2, "Irfan", " EMP0002")
        dt.Rows.Add(3, "Hrithik", " EMP0003")
        dt.Rows.Add(4, "Ranbir", " EMP0004")
        dt.Rows.Add(5, "Salman", " EMP0005")
        dt.Rows.Add(6, "Aamir", " EMP0006")
        dt.Rows.Add(7, "Akshay", " EMP0007")
        'Bind datatable to gridview
        grdEmployeeDetails.DataSource = dt
        grdEmployeeDetails.DataBind()
    End Sub
Now over to you:
"A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates." 
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..