Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Dynamically create datable and bind to gridview in asp.net

Mar 22, 2012
Introduction:

Here I will explain how to create datatable dynamically and bind to gridview in asp.net using c#, vb.net or create temporary table and bind data to columns in datatable dynamically using asp.net in c#, vb.net.


Description:

In Previous posts I explained lot of articles regarding
Asp.net, Gridview, SQL Server, Ajax, JavaScript etc. In many of articles I used datatable to bind data from database but in one situation I got requirement like dynamically build and bind data to datatable in asp.net.

To implement this requirement we need to create one new datatable after that add columns and bind data to columns after that add those columns to datatable before that first create new website open Default.aspx page and write the following code 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Dynamically create datatable and bind data to it in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvDetails" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
After that open code behind page and add the following namespace references


using System;
using System.Data;
After add namespaces write the following code in code behind

C# code


protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGridviewData();
}
}
/// <summary>
/// Dynamically create & bind data to datatable and bind datatable to gridview
/// </summary>
protected void BindGridviewData()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(Int32));
dt.Columns.Add("UserName", typeof (string));
dt.Columns.Add("Education", typeof (string));
dt.Columns.Add("Location",typeof(string));

DataRow dtrow = dt.NewRow();    // Create New Row
dtrow["UserId"] = 1;            //Bind Data to Columns
dtrow["UserName"] = "SureshDasari";
dtrow["Education"] = "B.Tech";
dtrow["Location"] = "Chennai";
dt.Rows.Add(dtrow);

dtrow = dt.NewRow();               // Create New Row
dtrow["UserId"] = 2;               //Bind Data to Columns
dtrow["UserName"] = "MadhavSai";
dtrow["Education"] = "MBA";
dtrow["Location"] = "Nagpur";
dt.Rows.Add(dtrow);

dtrow = dt.NewRow();              // Create New Row
dtrow["UserId"] = 3;              //Bind Data to Columns
dtrow["UserName"] = "MaheshDasari";
dtrow["Education"] = "B.Tech";
dtrow["Location"] = "Nuzividu";
dt.Rows.Add(dtrow);

dtrow = dt.NewRow();              // Create New Row
dtrow["UserId"] = 4;              //Bind Data to Columns
dtrow["UserName"] = "Mahendra";
dtrow["Education"] = "CA";
dtrow["Location"] = "Guntur";
dt.Rows.Add(dtrow);
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
VB.NET Code


Imports System
Imports System.Data
Partial Class Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

If Not IsPostBack Then
BindGridviewData()
End If
End Sub
' Dynamically create & bind data to datatable and bind datatable to gridview
Protected Sub BindGridviewData()
Dim dt As New DataTable()
dt.Columns.Add("UserId", GetType(Int32))
dt.Columns.Add("UserName", GetType(String))
dt.Columns.Add("Education", GetType(String))
dt.Columns.Add("Location", GetType(String))

Dim dtrow As DataRow = dt.NewRow()
' Create New Row
dtrow("UserId") = 1
'Bind Data to Columns
dtrow("UserName") = "SureshDasari"
dtrow("Education") = "B.Tech"
dtrow("Location") = "Chennai"
dt.Rows.Add(dtrow)

dtrow = dt.NewRow()
' Create New Row
dtrow("UserId") = 2
'Bind Data to Columns
dtrow("UserName") = "MadhavSai"
dtrow("Education") = "MBA"
dtrow("Location") = "Nagpur"
dt.Rows.Add(dtrow)

dtrow = dt.NewRow()
' Create New Row
dtrow("UserId") = 3
'Bind Data to Columns
dtrow("UserName") = "MaheshDasari"
dtrow("Education") = "B.Tech"
dtrow("Location") = "Nuzividu"
dt.Rows.Add(dtrow)

dtrow = dt.NewRow()
' Create New Row
dtrow("UserId") = 4
'Bind Data to Columns
dtrow("UserName") = "Mahendra"
dtrow("Education") = "CA"
dtrow("Location") = "Guntur"
dt.Rows.Add(dtrow)
gvDetails.DataSource = dt
gvDetails.DataBind()
End Sub
End Class
Demo



If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

9 comments :

Anonymous said...

nice article thank u for post....

abhi said...

can u give detail idea about resume parser project.. i need it urjent... thanks in advance

Anonymous said...

shailendra ....

I am great fan of your blog. I have learnt a lot in quick time from your blog. Thanxs a lot....... Carry on your good work

shashank said...

All articles are greate good work

mohanned khaild said...

it's really helpful , thanks

Mihir said...

Its nice one.. but how to append the data from coming from sql server and how to make grid to select a row and retrieve particular column value in that selected row?

Unknown said...

very good job. its really helpful to get new idea.

Anonymous said...

Thank you So Much....

Rajesh M Somvanshi

Anonymous said...

How to Insert data Dynamically in GridView..?

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.