• home
  • Email: trainings@synapseindia.careers

Blogs

back

Working with ASP.NET Table Control

Mar 11, 2016

ASP.NET Table control is totally different from available HTML table. ASP.NET Table is basically a server side control means you can pro-grammatically control it's flow and it renders into typical HTML table at page life cycle. The other major difference is that through HTML table you can create static table only with fixed rows and columns with fixed data and it can not be modified or created at server side as per the requirements of data to be shown. ASP.NET allows you to create table row and table cell pro-grammatically along with inserting data into it. You can also fill data fetched from SQL Server or any other RDBMS available into the table cell pro-grammatically. In this article , we will come to know about how table can be created at server side with adding rows and columns by using C# and ASP.NET. Take and example below where we have created a table with caption, heading and one row.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Table tb = new Table(); 		// creating a instance of table.
        tb.BorderWidth = Unit.Pixel(2);		// fixing the width of table.
        tb.BorderStyle = BorderStyle.Inset;	// fixing the border style of table.
        tb.GridLines = GridLines.Both;		// enabling both horizontal and vertical gridlines in table.
        tb.Caption = "Employee Details ";	// adding caption to table

        TableHeaderRow thr = new TableHeaderRow(); // creating Table Heading row equivalent to

        TableCell thc1 = new TableCell(); // adding table cells equivalent to
        thc1.Text = "Employee ID";

        TableCell thc2 = new TableCell();
        thc2.Text = "Employee Name";

        TableCell thc3 = new TableCell();
        thc3.Text = "Employee Department";


        thr.Controls.Add(thc1);
        thr.Controls.Add(thc2);
        thr.Controls.Add(thc3);

        TableRow tr1 = new TableRow();

        TableCell trc1 = new TableCell();
        trc1.Text = "E101";

        TableCell trc2 = new TableCell();
        trc2.Text = "Jitendra";

        TableCell trc3 = new TableCell();
        trc3.Text = "SALES";


        tr1.Controls.Add(trc1);
        tr1.Controls.Add(trc2);
        tr1.Controls.Add(trc3);


        tb.Controls.Add(thr);
        tb.Controls.Add(tr1);

        this.Controls.Add(tb);
    }

}

Above example is the simplest way of adding table. We have created a table at run time and also added some of its properties  at run time. Later we have created one table head with data and have added one table row also. After creating table head and table row, both are added into table. Finally a table is added into a a page.

SynapseIndia has many technologies under the list of SynapseIndia Technologies on which it is working . SynapseIndia is also providing training on project development on .NET to freshers of B.Tech and MCA.

SynapseIndia (CEO: Shamit Khemka)