1

I have successfully created generating new records into table rows but want to add table header, so how to do that?

private void GenerateTable(int rowsCount)
        {
            Table table = new Table();
            table.ID = "Table1";
            Page.Form.Controls.Add(table);
            const int colsCount = 3;
            for (int i = 0; i < rowsCount; i++)
            {
                TableRow row = new TableRow();
                for (int j = 0; j < colsCount; j++)
                {                    
                    TableCell cell = new TableCell();
                    TextBox tb = new TextBox();
                    tb.ID = "TextBoxRow_" + i + "Col_" + j;
                    cell.Controls.Add(tb);
                    row.Cells.Add(cell);
                }
                table.Rows.Add(row);
            }
            SetPreviousData(rowsCount, colsCount);
            rowsCount++;
            ViewState["RowsCount"] = rowsCount;
        }
1
  • 1
    put a new TableRow() outside of your for() loop and put in the header fields there. Commented May 27, 2015 at 14:14

2 Answers 2

3

You do it like that:

TableHeaderRow header = new TableHeaderRow(); // Creating a header row
table.Rows.Add(header); // Add the header row to table tbl 

For adding cells in the header row here is an example:

 TableHeaderCell headerTableCell1 = new TableHeaderCell();
 header.Cells.Add(headerTableCell1);

And this is where is goes in your code:

private void GenerateTable(int rowsCount)
        {
            Table table = new Table();
            table.ID = "Table1";
            Page.Form.Controls.Add(table);
            const int colsCount = 3;

             TableHeaderRow header = new TableHeaderRow(); 
             table.Rows.Add(header); 
             //These two lines in iteir own loop
             TableHeaderCell headerTableCell1 = new TableHeaderCell();
             header.Cells.Add(headerTableCell1);
            for (int i = 0; i < rowsCount; i++)
            {
                TableRow row = new TableRow();
                for (int j = 0; j < colsCount; j++)
                {                    
                    TableCell cell = new TableCell();
                    TextBox tb = new TextBox();
                    tb.ID = "TextBoxRow_" + i + "Col_" + j;
                    cell.Controls.Add(tb);
                    row.Cells.Add(cell);
                }
                table.Rows.Add(row);
            }
            SetPreviousData(rowsCount, colsCount);
            rowsCount++;
            ViewState["RowsCount"] = rowsCount;
        }
Sign up to request clarification or add additional context in comments.

6 Comments

how to assign header name for columns?
I mean in my code where to put code for add header for all first column cells?and secode and so on???
then where is header name assigned?
when you create each tableHeadearCell, you add it, for example: headerTableCell1.Text
i am creating three columns so creating three header cells so how to add this three header cells to three columns?
|
0

Building on the suggested solution by @israel-altar, I struggled with adding the header into a <thead> tag and not into a <tbody>.

To achieve this, you have to use the TableSelection property of your TableHeaderRow:

private void GenerateTable(List<string> fieldsList)
{
    Table table = new Table();
    table.ID = "Table1";
    Page.Form.Controls.Add(table);
    
    TableHeaderRow header = new TableHeaderRow(); 
    foreach (string field in fieldsList)
    {
        TableHeaderCell headerTableCell1 = new TableHeaderCell();
        headerTableCell1.Controls.Add(new Literal() {
            Text = field
        });
        header.Cells.Add(headerTableCell1);
    }

    // Set TableSection to TableHeader so that the TableHeaderRow is added within <thead> and not within <tbody>
    header.TableSection = TableRowSection.TableHeader;
    
    table.Rows.Add(header); 
    
    // loop rows ...
}

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.