Tuesday, 18 December 2012

Create DataTable Manually bind it to a Gridview


Create Datatable Manually bind it to a Gridview


Design Part:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

        </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 Default6 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        DataSet _ds = new DataSet();

        DataTable _dt = new DataTable();

        DataRow _dr;

        try

        {

            _dt.Columns.Add(new DataColumn("srno", typeof(int)));

            _dt.Columns.Add(new DataColumn("Name", typeof(string)));

            _dt.Columns.Add(new DataColumn("Address", typeof(string)));

            _dt.Columns.Add(new DataColumn("MobileNo", typeof(int)));

            _dt.Columns.Add(new DataColumn("EmailAddress", typeof(string)));

            _dr = _dt.NewRow();

            _dr[0] = 1;

            _dr[1] = "Name1";

            _dr[2] = "Address1";

            _dr[3] = 93221489;

            _dr[4] = "EmailAddress1";

            _dt.Rows.Add(_dr);

            _ds.Tables.Add(_dt);

            GridView1.DataSource = _ds;

            GridView1.DataBind();

        }

        catch(Exception ex)

        {

        }

    }   

}
 

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top