2

is it possible to dynamically create html table columns (< th>< /th>) using namespae System.Web.UI.HtmlControls ?

3 Answers 3

3

try this:

HtmlTable table1 = new HtmlTable();

        // Set the table's formatting-related properties.
        table1.Border = 1;
        table1.CellPadding = 3;
        table1.CellSpacing = 3;
        table1.BorderColor = "red";

        // Start adding content to the table.
        HtmlTableRow row;
        HtmlTableCell cell;
        for (int i=1; i<=5; i++)
        {
                // Create a new row and set its background color.
                row = new HtmlTableRow();
                row.BgColor = (i%2==0 ? "lightyellow" : "lightcyan");
                for (int j=1; j<=4; j++)
                {
                        // Create a cell and set its text.
                        cell = new HtmlTableCell();
                        cell.InnerHtml = "Row: " + i.ToString()+ "<br />Cell: " + j.ToString();
                        // Add the cell to the current row.
                        row.Cells.Add(cell);
                }

                // Add the row to the table.
                table1.Rows.Add(row);
        }

        // Add the table to the page.
        this.Controls.Add(table1);
Sign up to request clarification or add additional context in comments.

Comments

2

Yes in that name space you will want to look at:

HtmlTable
HtmlTableRow
HtmlTableCell

Use is like so:

        HtmlTable htmlTable = new HtmlTable();    //creating table object     
        HtmlTableRow htmlRow = new HtmlTableRow();   //creating row object
            HtmlTableCell htmlTableCell = new HtmlTableCell();   //create cell
            htmlTableCell.InnerHtml = "<b>Test Text Bolded</b>";//setting cell content
            HtmlTableCell textTableCell = new HtmlTableCell();  //create cell
            textTableCell.InnerText = "Test plain text";  //setting cell content

        htmlRow.Cells.Add(htmlTableCell);   //add cell to row
        htmlRow.Cells.Add(textTableCell);   //add cell to row
        htmlTable.Rows.Add(htmlRow);   // add row to table

Comments

2
HtmlTableCell = new HtmlTableCell("th");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.