leora
leora

Reputation: 196499

datatable with foreach loop

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.

Upvotes: 0

Views: 2243

Answers (2)

Mutation Person
Mutation Person

Reputation: 30498

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!

Upvotes: 0

Guffa
Guffa

Reputation: 700342

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
}

Upvotes: 3

Related Questions