GKra
GKra

Reputation: 37

Use dynamic table to display list of string

I tried to display the list of time slots by using dynamic table in C#. However, it does not work properly. Here is my result: enter image description here

And here is my code

List<AvailableTime> AllTimeSlots = RequestDirector.ListAllAvailableTimes(BookingDate);
    foreach (AvailableTime resultTimeslot in AllTimeSlots)
    {
        TableRow TimeSLotRow = new TableRow();
        TableCell TimeSlotCell = new TableCell();
        TimeSlotCell.Text = Convert.ToString(resultTimeslot.TimeSlot);



        TimeSLotRow.Cells.Add(TimeSlotCell);

        Table1.Rows.Add(TimeSLotRow);
    }

I want to display 5 time slots each row. Can anybody tell me how I can do that?

Upvotes: 0

Views: 375

Answers (1)

Antoine Pelletier
Antoine Pelletier

Reputation: 3316

Probably something like this :

int i = 0;
TableRow TimeSLotRow = new TableRow();
foreach (AvailableTime resultTimeslot in AllTimeSlots)
{

    TableCell TimeSlotCell = new TableCell();
    TimeSlotCell.Text = Convert.ToString(resultTimeslot.TimeSlot);



    TimeSLotRow.Cells.Add(TimeSlotCell);

    i++;

    if(i == 5)
    {

        Table1.Rows.Add(TimeSLotRow);
        TimeSLotRow = new TableRow();
        i = 0;
    }
}

Upvotes: 1

Related Questions