使用动态表显示字符串列表

时间:2017-02-27 19:31:01

标签: c# asp.net dynamic-tables

我尝试在C#中使用动态表来显示时隙列表。但是,它无法正常工作。这是我的结果: enter image description here

这是我的代码

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);
    }

我想每行显示5个时段。有人能告诉我怎么做吗?

1 个答案:

答案 0 :(得分:1)

可能是这样的:

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;
    }
}
相关问题