Posted by : Anwar Hossain
Category : DataTable example in ASP.Net C#

How to Create DataTable using ASP.NET C#

DataTable Example using ASP.NET C#

Dear viewer I will show how to create DataTable in ASP.NET C#. DataTable is used to store data in memory. Hear I have created a DataTable with five columns of type int, string and DateTime. After createing DataTable I have added some row to the DataTable and filled up with data.I have used a gridView to display DataTable value.

DataTable Example using ASP.NET C#

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Datatable in asp.net c #</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <asp:GridView ID="gvEmployee" CssClass="Grid" runat="server"
  AutoGenerateColumns="False" BackColor="White" BorderColor="White"
  BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1"
  GridLines="None">
  <Columns>
  <asp:BoundField DataField="StudentName" HeaderText="StudentName" />
  <asp:BoundField DataField="Address" HeaderText="Address" />
  <asp:BoundField DataField="Phone" HeaderText="Phone" />
  <asp:BoundField DataField="JoingDate" HeaderText="JoingDate" />
 
  </Columns>
  <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
  <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
  <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
  <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
  <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
  <SortedAscendingCellStyle BackColor="#F1F1F1" />
  <SortedAscendingHeaderStyle BackColor="#594B9C" />
  <SortedDescendingCellStyle BackColor="#CAC9C9" />
  <SortedDescendingHeaderStyle BackColor="#33276A" />
  </asp:GridView>
  </div>
  </form>
</body>
</html>

CODE BEHIND

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
 
public partial class DatatableExample : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
 
  if (!IsPostBack)
  {
 
  BindGrid();
 
  }
 
  }
  protected void BindGrid()
  {
   gvEmployee.DataSource = GetEmployee();
  gvEmployee.DataBind();
  }
  static DataTable GetEmployee()
  {
 
 
 
  DataTable table = new DataTable();
  table.Columns.Add("ID", typeof(int));
  table.Columns.Add("StudentName", typeof(string));
  table.Columns.Add("Address", typeof(string));
  table.Columns.Add("Phone", typeof(string));
  table.Columns.Add("JoingDate", typeof(DateTime));
 
 
  table.Rows.Add(1,"Asraful Alum", "Dhaka","01856517548","11/5/2010");
  table.Rows.Add(2,"Ahsan", "Chittagong", "01856587548","12/5/2012");
  table.Rows.Add(3,"Nirub", "Sylhet", "01856587644","11/10/2008");
  table.Rows.Add(4,"Salim Zaman", "Rajsahi", "01856587578","11/15/2007");
  table.Rows.Add(5,"Habib", "Barisal", "01856987548","11 /5/2008");
  table.Rows.Add(6,"Roman", "Chandpur", "01856587588","11/5/2010");
  table.Rows.Add(7,"Iqbal Hossain", "Noakhali","01856587148","11/5/ 2010");
 
 
  return table;
  }
}

Out Put

Data-table-in-ASP.Net



Realted Article Headline

Store DataTable in session using ASP.Net C Sharp
How to Create DataTable using ASP.NET C#

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection