ASP.NET - dynamically create table control to show Hierarchy

Asked By anbu n on 30-Jan-11 11:56 AM
how to dynamically create table control to show Hierarchy view of data s

for eg:


            child1                          child2
       subchild1    subchild2        subchild1 subchild2
Peter Bromberg replied to anbu n on 30-Jan-11 12:53 PM
You will need to determine how to parse your hierarchical data, but this is the general technique to build a table programmatically and add it to a Placeholder control:

 private void CreateDynamicTable()
    // Fetch the number of Rows and Columns for the table 
    // using the properties
    int tblRows = Rows;
    int tblCols = Columns;
    // Create a Table and set its properties 
    Table tbl = new Table();
    // Add the table to the placeholder control
    // Now iterate through the table and add your controls 
    for (int i = 0; i < tblRows; i++)
      TableRow tr = new TableRow();
      for (int j = 0; j < tblCols; j++)
        TableCell tc = new TableCell();
        TextBox txtBox = new TextBox();
          txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;
          // Add the control to the TableCell
        // Add the TableCell to the TableRow
      // Add the TableRow to the Table
     // This parameter helps determine in the LoadViewState event,
     // whether to recreate the dynamic controls or not
     ViewState["dynamictable"] = true;