Adding Rows and Cells Dynamically to a Table Control in C#

From the Toolbox, drag a Table controlonto your page. We willadd a PreInit event to the page and write a for loop to create five new rows in the table and add another for loop to create three columns for each row.

protected void Page_PreInit(object sender, EventArgs e)  
  Table1.BorderWidth = 1; 
  for (int row = 0; row < 5; row++) 
    TableRow tr = new TableRow(); 
    for (int column = 0; column < 3; column++) 
      TableCelltc = new TableCell(); 
      tc.Text = string.Format("Row: {0} Cell: {1}", row, column); 
      tc.BorderWidth = 1; 
      tr.Cells. Add(tc); 
    Table1. Rows. Add(tr); 

Enjoyed this post? Share it!


Leave a comment

Your email address will not be published.