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

Total Post:341

Points:2389
Posted on    January-12-2016 9:58 PM

 C# C#  ADO.Net  ASP.Net 
Ratings:


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


aditya kumar Patel

Total Post:242

Points:1722
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">

<title></title>

</head>

<body>

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

<div>

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

<Columns>

<asp:TemplateField HeaderText="CustomerID">

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

</asp:TemplateField>

<asp:TemplateField HeaderText="CustomerName">

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

</asp:TemplateField>

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

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

</asp:TemplateField>

<asp:TemplateField HeaderText="Address">

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

</asp:TemplateField>

</Columns>

</asp:GridView>

</div>

</form>

</body>

</html>

 

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)

 

 

{

FillGridviewData();

}

}

 

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";

 

 

dt.Rows.Add(dtrow);

dtrow = dt.NewRow();

 

dtrow["CustomerId"] = 2;

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

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

dtrow["Address"] = "allahabad";

 

 

dt.Rows.Add(dtrow);

dtrow = dt.NewRow();

 

dtrow["CustomerId"] = 3;

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

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

dtrow["Address"] = "thane";

 

 

dt.Rows.Add(dtrow);

GridView1.DataSource = dt;

GridView1.DataBind();

}

}

}

 

 

 


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

Follow MindStick