Home > DeveloperSection > Forums > How to use Datatable in asp.net
Ankit Singh

Total Post:341

Posted on    January-12-2016 9:58 PM

 C# C#  ADO.Net  ASP.Net 

 1 Reply(s)
 159  View(s)
Rate this:
I want to use datatable in asp.net. How to do this please help me.

aditya kumar Patel

Total Post:250

Posted on    January-12-2016 10:03 PM

A datatable is a collection of rows.which is represent the row data.
The DataTable is a object in the ADO.NET library.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataSet.aspx.cs" Inherits="Forumasp.DataSetDemo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">




<form id="form1" runat="server">


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">


<asp:TemplateField HeaderText="CustomerID">

<ItemTemplate><%#Eval("CustomerID") %></ItemTemplate>


<asp:TemplateField HeaderText="CustomerName">

<ItemTemplate><%#Eval("CustomerName") %></ItemTemplate>


<asp:TemplateField HeaderText="E-mailaddress">

<ItemTemplate><%#Eval("Email") %></ItemTemplate>


<asp:TemplateField HeaderText="Address">

<ItemTemplate><%#Eval("Address") %></ItemTemplate>









using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

namespace Forumasp





public partial class DataSetDemo : System.Web.UI.Page





protected void Page_Load(object sender, EventArgs e)





if (!IsPostBack)








protected void FillGridviewData()





DataTable dt = new DataTable();

dt.Columns.Add("CustomerId", typeof(Int32));

dt.Columns.Add("CustomerName", typeof(string));

dt.Columns.Add("Email", typeof(string));

dt.Columns.Add("Address", typeof(string));

DataRow dtrow = dt.NewRow();

dtrow["CustomerId"] = 1;

dtrow["CustomerName"] = "Suraj Dasari";

dtrow["Email"] = "adityasingh06630@gmail.com";

dtrow["Address"] = "mungra phulpur";




dtrow = dt.NewRow();


dtrow["CustomerId"] = 2;

dtrow["CustomerName"] = "manoj kumar";

dtrow["Email"] = "manoj06630@gmail.com";

dtrow["Address"] = "allahabad";




dtrow = dt.NewRow();


dtrow["CustomerId"] = 3;

dtrow["CustomerName"] = "surendra kumar";

dtrow["Email"] = "surendra@gmail.com";

dtrow["Address"] = "thane";




GridView1.DataSource = dt;








Don't want to miss updates? Please click the below button!

Follow MindStick