Saturday, 3 August 2013

DataTable Example for Dynamically Create Datatable and Bind to Gridview in Asp.Net




Description:-

                        In this Example we Explain that How to Dynamically Create Datatable and Bind it to Gridview. We are create a one DataTable object and one DataRow object and we Create one New Row and add it to Datatable like.

Here is we create DataTable object and add Four Coloum to this datatable like

DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(Int32));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("age", typeof(Int32));
        dt.Columns.Add("gender", typeof(string));

Here is we create DataRow object and Create a New Row and add this Row to the DataTable object.

  DataRow dtrow = dt.NewRow();    // Create New Row
        dtrow["Id"] = 1;            //Bind Data to Columns
        dtrow["Name"] = "kirit";
        dtrow["age"] = "24";
        dtrow["gender"] = "Male";
        dt.Rows.Add(dtrow);

what is DataTable:-

                        DataTable is a central object in the ADO.NET library. Through DataTable you can easily create Table Dynamically and add to the database.

what is DataRow:-

                        DataRow is also a Class derieved from System.Data through DataRow you can Create a DataRow and add to the DataTable.


Implement Remember Me functionality using CheckBox ASP.Net 

set WaterMark Text in PDF using itextsharp in C#

How to set Default Button in MVC Web Form Application 

How to Bind XML File data to Treeview in asp.net

JQuery datepicker calender with Dropdown month and year in asp.net.
to show Example of How to Rotate the Ads or Advertised without Refreshing the page Rotate Ads in Asp.Net


Default.aspx:-



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



<!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"></asp:GridView>
    </div>
    </form>
</body>
</html>

 


Default.aspx.cs:-



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 _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(Int32));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("age", typeof(Int32));
        dt.Columns.Add("gender", typeof(string));

        DataRow dtrow = dt.NewRow();    // Create New Row
        dtrow["Id"] = 1;            //Bind Data to Columns
        dtrow["Name"] = "kirit";
        dtrow["age"] = "24";
        dtrow["gender"] = "Male";
        dt.Rows.Add(dtrow);

        dtrow = dt.NewRow();    // Create New Row
        dtrow["Id"] = 2;            //Bind Data to Columns
        dtrow["Name"] = "pintu";
        dtrow["age"] = "24";
        dtrow["gender"] = "Male";
        dt.Rows.Add(dtrow);

         dtrow = dt.NewRow();    // Create New Row
        dtrow["Id"] = 3;            //Bind Data to Columns
        dtrow["Name"] = "reema";
        dtrow["age"] = "22";
        dtrow["gender"] = "FeMale";
        dt.Rows.Add(dtrow);

        GridView1.DataSource = dt;
        GridView1.DataBind();

    }
}
  
 

0 comments:

Post a Comment