HOW TO USE DATATABLE IN ASP.NET

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C#
 404  View(s)
Ratings:
Rate this:
I want to use datatable in asp.net. How to do this please help me.
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: How to use Datatable in asp.net

    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();

    }

    }

    }

     

     

     

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!