ASP.Net C# Code to Create Dynamic Tables

In ASP.Net 2.0 there are number of controls to display the data retrieved from the database in tabular form using Grid View Control, list view Control, Repeater. But in some cases when there is no option to custom these default controls. At that time a questions arises – how to generate dynamic tables? How to display the data according to requirement?
You can use the Literal Control to display the html tagged string generated by using VB/C# code in ASP.Net 2.0.
To generate the table you can use the following C# code:
To generate the table you can use the following C# code:

 private static string GenerateTable()
        {
            // create a string type variable to generate dynamic table
            string dynTable = "";
            // start with table tag with following attributes
            dynTable = "<table cellspacing=\"0\" cellpadding=\"2\" border=\"1\">";
            // outer loop to generate table rows
            for (int tRows = 0; tRows < 5; tRows++)
            {
                //start table row
                dynTable += "<tr>";
                // inner loop to generate columns
                for (int tCols = 0; tCols < 4; tCols++)
                {
                    // create column
                    dynTable += "<td>"; dynTable += "Row: " + (tRows + 1) + " Col: " + (tCols + 1);
                    // close td column tag
                    dynTable += "</td>";
                }
                // close table row
                dynTable += "</tr>";
            }
            // close the table tag
            dynTable += "</table>";
            return dynTable;
        }
Literal1.Text = GenerateTable();
Above C# code will build a string having table tag, tr as table row, td as table data/column. To display the data retrieved from database you can set the tRows < [No. of DataRows Retrieved] and tCols < [No. of DataColmns].
Output Result of above code:
Row: 1 Col: 1 Row: 1 Col: 2 Row: 1 Col: 3 Row: 1 Col: 4
Row: 2 Col: 1 Row: 2 Col: 2 Row: 2 Col: 3 Row: 2 Col: 4
Row: 3 Col: 1 Row: 3 Col: 2 Row: 3 Col: 3 Row: 3 Col: 4
Row: 4 Col: 1 Row: 4 Col: 2 Row: 4 Col: 3 Row: 4 Col: 4
Row: 5 Col: 1 Row: 5 Col: 2 Row: 5 Col: 3 Row: 5 Col: 4
Reactions

Post a Comment

3 Comments

  1. I think your example would read better if you used the built-in controls for creating tables:

    HtmlTable t = new HtmlTable();
    this.Controls.Add(t);
    for(int i = 0; i < 25; i++) {
    HtmlTableRow tr = new HtmlTableRow();
    t.Rows.Add(tr);
    for(int j = 0; j < 25; j++) {
    HtmlTableCell td = new HtmlTableCell();
    tr.Cells.Add(td);
    td.InnerText = string.Format("{0},{1}", i, j);
    }
    }

    ReplyDelete
  2. thanks for sharing ur code and idea?

    ReplyDelete
  3. Thanks for sharing!

    Had a hard time finding out how to do this!

    ReplyDelete

Please do not post any spam link in the comment box😊

Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)

Close Menu