Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 1st, 2012  |  syntax: None  |  size: 1.46 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ASP.NET C# Generate Table Dynamically doesn't work
  2. using System.Data;
  3.  
  4. // Create a DataTable instance
  5. DataTable dTbl = new DataTable("myDynamicTable");
  6.  
  7. // Create a DataColumn instances
  8.  
  9. DataColumn dValue = new DataColumn();
  10. DataColumn dMember = new DataColumn();
  11.  
  12. dValue.ColumnName = "Id";
  13. dValue.DataType = Type.GetType("System.Int32");
  14.  
  15. dMember.ColumnName = "Name";
  16. dMember.DataType = Type.GetType("System.String");
  17.  
  18. // Add these DataColumns into the DataTable
  19.  
  20. dTbl.Columns.Add(dValue);
  21.  
  22. dTbl.Columns.Add(dMember);
  23. DataRow myrow = dTbl.NewRow();
  24.  
  25. myrow["Id"] = 1;
  26. myrow["Name"] = "Tux";
  27.  
  28. // Add the row into the table
  29.  
  30. dTbl.Rows.Add(myrow);
  31.        
  32. HtmlTable table1 = new HtmlTable();
  33.  
  34. // Set the table's formatting-related properties.
  35. table1.Border = 1;
  36. table1.CellPadding = 3;
  37. table1.CellSpacing = 3;
  38. table1.BorderColor = "red";
  39.  
  40. // Start adding content to the table.
  41. HtmlTableRow row;
  42. HtmlTableCell cell;
  43. for (int i = 1; i <= 5; i++)
  44. {
  45.     // Create a new row and set its background color.
  46.     row = new HtmlTableRow();
  47.     row.BgColor = (i % 2 == 0 ? "lightyellow" : "lightcyan");
  48.  
  49.     for (int j = 1; j <= 4; j++)
  50.     {
  51.         // Create a cell and set its text.
  52.         cell = new HtmlTableCell();
  53.         cell.InnerHtml = "Row: " + i.ToString() +
  54.           "<br>Cell: " + j.ToString();
  55.  
  56.         // Add the cell to the current row.
  57.         row.Cells.Add(cell);
  58.     }
  59.  
  60.     // Add the row to the table.
  61.     table1.Rows.Add(row);
  62. }
  63.  
  64. // Add the table to the page.
  65. this.Controls.Add(table1);