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

Asp.net Create DataTable Dynamically and Bind to Gridview in C#

Jul 22, 2014
Introduction

Here I will explain how to create datatable dynamically and bind to gridview in asp.net using c#, vb.net or bind asp.net gridview with dynamically created table in 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.netGridviewSQL ServerAjaxJavaScript 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 ObjectByVal 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

3 comments :

Anonymous said...

javascript:alert('A nice Blog!!');

Anonymous said...

Good article..

Stephen Ayodeji said...

Great article, but can you demonstrate adding an image via this process?

Give your Valuable Comments

Other Related Posts

© 2010-2012 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.