Dynamically Create Temporary Table in Asp.net

Dynamically Create Temporary Table in Asp.net using C#:



In this post we describe working with c# code and create tem or temporary table in asp.net using C# code. It is very simple to make HTML controls or asp.net web controls before running a Web Page. In asp.net Just Drag and Drop from Toolbox menu or write ASP code for that asp.net web Page (abc.aspx). But some time we think that generating HTML controls or asp.net controls are difficult during runtime.

Dynamically Temporary Table creation in Asp.net:

Here we give a segment of code in this we make an asp.net table (data table) by C# code.
Create Temporary Table by C# in asp.net:

DataTable dt = new DataTable();

Create the object of datatable class.now

        dt.Columns.Add("Cost_Id", typeof(Int32));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Address", typeof(string));

Add column in data table object. And last fill the values in these columns.

        dt.Rows.Add(1, "Parijat", "India");

Using C# code Create a Table Dynamically in ASP.NET:

Temporary tablebyCsharp.aspx code Example:

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

<!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>
    <title>Creating a temp table in asp.net using c#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <h1>
            Example of Creating a temp data table in asp.net using c#</h1>
        <br />
        <%--Take a grid control and bind this control by dymanic created table.--%>
        <asp:GridView ID="gird1" runat="server" Height="236px" Width="462px">
            <HeaderStyle BackColor="Maroon" Font-Bold="true" ForeColor="White" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>


C# code on table by Csharp.aspx.cs for create table:

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 tablebyCsharp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();

        //create colums here.
        dt.Columns.Add("Cost_Id", typeof(Int32));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Address", typeof(string));

        //fill the value in colums by row.
        dt.Rows.Add(1, "Parijat", "India");
        dt.Rows.Add(2, "Roz", "U.S.A");
        dt.Rows.Add(3, "Maqsood", "U.P.");
        dt.Rows.Add(4, "Parvez", "Pakistan");
        dt.Rows.Add(6, "Tinny", "Japan");
        dt.Rows.Add(7, "Mr. Song", "Chine");

       //bind dymanic created table in grid.
        gird1.DataSource = dt;
        gird1.DataBind();
       
    }
} 
Dynamically Create Temporary Table in Asp.net
 Temporary Table in Asp.net

Other Related Post on this tutorial:

·          How to Bind Gridview Form database.

Comments