views:

278

answers:

2

i have a data table and i want to write a loop where i can render a html table and i want to do it from scratch (not abstracted data sources)

i want to have the number of items per row a variable.

what is the correct loop syntax given a datatable with X number of records where each record is one cell.

so if i have 20 records and my NumberOfItemsPerRow = 5, i would have a html table with 4 rows.

+3  A: 

This is how you loop to create a table with the available data. The last row is completed with empty cells to make a full row.

int index = 0;
while (index < theDataTable.Rows.Count) {
   // start of table row
   for (int column = 0; column < numberOfColumns; i++) {
      if (index < theDataTable.Rows.Count) {
         // table cell with data from theDataTable.Rows[index]
      } else {
         // empty cell
      }
      index++;
   }
   // end of table row
}
Guffa
A: 

Using a JavaScript library can help also,

For example, in jQuery:

$("#theDataTable tr").each(function(){   //loop though rows
    $(this).find("td").each(function(){  //loops through cells
    });
});

Much less code!

James Wiseman
i am using asp.net mvc so doesn't it make sense to break this code outside the html and into a viewhelper or even the controller
ooo