Ad

Monday, July 1, 2013

How To Create a Data Table Programatically Using C#.Net,ASP.Net?

In this article i would like to explore the concept of creating a Data Table Using C#,Asp.Net.

Overview:
                Generally we can get the data from data base we can represent in any of the databound controls like gridview,form view,list view etc..........



                  In this article i would like to pass the data to any of the controls an that data should be presented to the gridview.For this i would like to consider datatable.

Description:
                   Data Table is a collection of rows and columns

For working of Data Table we need to give an instance................
                      DataTable dt=new DataTable();
Now You need to declare columns also
                      DataColumn dc=new DataColumn("ColumName",DataType");
                                                       (or)
                      DataColumn col1=new DataColumn("ID");
                      col1.DataType=System.Type.GetType("System.String");

We can add to DataTable
                        dt.Columns.Add(dc); 
                                 (or)
                         dt.Columns.Add(col1);
Creating a Row in the DataTable
                             DataRow dr=dt.NewRow();
Fill the Data in the DataRow
                             dr[0]="Hi";
Add the Row into the DataTable
                            dt.Rows.Add(dr);

Program:

Design Page(Source Code):

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





    Data Table Page
   
   

Example On DataTable Using C#,Asp.Net

   

   
   
   

   
   
Enter Your Name

   


   
   
   
Enter Qualification

   


   
   
   
Enter Your Designation

   


   
   
   


   


   
   
   
   
   
            BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
       
       
       
       
       
   
   
   
       
   




Design View:

Coding:

using System;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
  
    protected void b1_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        DataColumn dc = new DataColumn("Name", typeof(string));
        dt.Columns.Add(dc);
        dt.Columns.Add("Qualification", typeof(string));
        dt.Columns.Add("Designation", typeof(string));
        //dt.Rows.Add(txtname.Text, txtQuali.Text, txtDes.Text);
        DataRow dr = dt.NewRow();
        dr[0] = txtname.Text;
        dr[1] = txtQuali.Text;
        dr[2] = txtDes.Text;
        dt.Rows.Add(dr);
        g1.DataSource = dt;
        g1.DataBind();
    }
}

OutPut:




                             

No comments: