Runtime Create a DataTable and Bind it to a GridView in C# VB.Net

DataTable is one of the most used object under System.Data namespace. To create a DataTable programmatically you have to initialize a new instance of the DataTable class with arguments or even without arguments. DataTable has 4 Constructors. Mostly used constructors are DataTable() and DataTable(String). Where String argument accept a Table Name. If you don’t provide any table name then table name will be an incremental default name “TableN” like Table0. In other two constructors you can pass SerializationInfo and specified table’s namespace. After initializing the DataTable, we need to define its schema by adding DataColumn objects to the DataColumnCollection. To add rows to a DataTable, you must first use the NewRow method to create a new DataRow object. The NewRow method returns a row with the schema of the DataTable. The maximum number of rows of a DataTable can be 16,777,216. Note that the DataTable can be bound to any data Web control like DataSet or DataReader as a datasource. When binding a DataTable to a data Web control, in actuality, the default DataTable is used as a datasource to that control like GridView, Repeater, Dropdown List etc. Here in this Asp.Net Tutorial i will discuss on how to create a DataTable in runtime & bind this DataTable into a Gridview. I will also provide the code example to download in both C# & VB.Net format for your practice purpose.

Creating DataTable in Run Time:
To create a DataTable in runtime first instantiate the DataTable object then add columns with data types & then assign values to create rows. To do this please follow the simple example from below:

C# Code:

            DataTable oTable = new DataTable("Article");

            //Add DataTable column dynamically/run time/on the fly.
            oTable.Columns.Add(new DataColumn("ID", typeof(System.Int64)));
            oTable.Columns.Add(new DataColumn("Title", typeof(System.String)));
            oTable.Columns.Add(new DataColumn("Published", typeof(System.DateTime)));

            //Add DataTable rows dynamically/run time/on the fly.
            oTable.Rows.Add(1001, "DataTable Engineering", DateTime.Now);
            oTable.Rows.Add(1002, "Event Calendar", DateTime.Now);
            oTable.Rows.Add(1003, "Master Detail Data", DateTime.Now);

Note: Don’t forget to add “using System.Data;” namespace.

VB.Net Code:

            Dim oTable As DataTable = New DataTable("Article")

            'Add DataTable column dynamically/run time/on the fly.
            oTable.Columns.Add(New DataColumn("ID", System.Type.GetType("System.Int64")))
            oTable.Columns.Add(New DataColumn("Title", System.Type.GetType("System.String")))
            oTable.Columns.Add(New DataColumn("Published", System.Type.GetType("System.DateTime")))

            'Add DataTable rows dynamically/run time/on the fly.
            oTable.Rows.Add(1001, "DataTable Engineering", DateTime.Now)
            oTable.Rows.Add(1002, "Event Calendar", DateTime.Now)
            oTable.Rows.Add(1003, "Master Detail Data", DateTime.Now)

Note: Don’t forget to add “Imports System.Data” namespace.

Available DataTypes for DataTable:
1. Boolean
2. Byte
3. Char
4. DateTime
5. Decimal
6. Double
7. Int16
8. Int32
9. Int64
10. SByte
11. Single
12. String
13. TimeSpan
14. UInt16
15. UInt32
16. UInt64

Now add a GridView Control in HTML Markup:

    <asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" CellPadding="5">

Bind DataTable into GridView Control:
C# Code:

            GridView1.DataSource = oTable;

VB.Net Code:

            GridView1.DataSource = oTable

Now run the project. Hope you will get an output like below:

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

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

AlphaOmega Captcha Classica  –  Enter Security Code