Fill ASP.NET Table with data from DataTable.

Few days ago, one of the members at asp.net forums ask if how to display the data that comes from the DataTable to a Table, so I decided to post the solution that I have provided there as a reference to others.

In this example I’m going to create a DataTable by hand and define the columns and row values manually and then display data to the Table.

Here are the code blocks below:

  private DataTable CreateDataTable()

  {

        DataTable dt = new DataTable();

        DataRow dr = null;

        //Create the Columns Definition

        dt.Columns.Add(new DataColumn("Column1", typeof(string)));

        dt.Columns.Add(new DataColumn("Column2", typeof(string)));

        dt.Columns.Add(new DataColumn("Column3", typeof(string)));

        //Add the first Row to each columns defined

        dr = dt.NewRow();

        dr["Column1"] = "A";

        dr["Column2"] = "B";

        dr["Column3"] = "C";

        dt.Rows.Add(dr);

        //Add the second Row to each columns defined

        dr = dt.NewRow();

        dr["Column1"] = "D";

        dr["Column2"] = "E";

        dr["Column3"] = "F";

        dt.Rows.Add(dr);

        //You can continue adding rows here

        return dt;

    }

    private void GenerateTable()

    {

        DataTable dt = CreateDataTable();

        Table table = new Table();

        TableRow row = null;

        //Add the Headers

        row = new TableRow();

        for (int j = 0; j < dt.Columns.Count; j++)

        {

            TableHeaderCell headerCell = new TableHeaderCell();

            headerCell.Text = dt.Columns[j].ColumnName;

            row.Cells.Add(headerCell);

        }

        table.Rows.Add(row);

        //Add the Column values

        for (int i = 0; i < dt.Rows.Count; i++)

        {

            row = new TableRow();

            for (int j = 0; j < dt.Columns.Count; j++)

            {

                TableCell cell = new TableCell();

                cell.Text = dt.Rows[i][j].ToString();

                row.Cells.Add(cell);

            }

            // Add the TableRow to the Table

            table.Rows.Add(row);

        }

        // Add the the Table in the Form

        form1.Controls.Add(table);

    }

    protected void Page_Load(object sender, EventArgs e)

    {

        GenerateTable();

    }

Running the code above will show this output below in the page.

FillTable

That’s it! I hope you will find this example useful

This article is part of the GWB Archives. Original Author: Vincent Maverick Durano

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.