我正在从数据库打印并使用循环。我已经在asp中完成了这个,但我正在改为c#asp.net并遇到麻烦。
首先,我打印日程表标题
时间|法院|法院|法庭根据法院的数量,它打印游戏。 接下来,当前记录日期与在整个表格行上打印日期的最后日期不同。 然后它检查当前记录的时间是否与最后一个相同,如果它不打印时间,然后是游戏记录,如果它只是打印游戏记录。
我的问题是我在时间中声明TableRow,如果我在另一个语句中尝试使用它,那么它超出了范围。如果我在if语句之外使用tablerow,它就不能正确打印。
这就是我所拥有的。
for (int i = 0; i < GameCount; i++)
{
DateTime currentdatetime = (DateTime)Schedules.Tables["Schedule"].Rows[i]["datetime"];
string ndate = currentdatetime.ToString("MM/dd/yyy");
string ntime = currentdatetime.ToString("HH:mm");
string nextdate = currentdatetime.ToString("MM/dd/yyy");
if (i + 1 != GameCount)
{
DateTime nextdatetime = (DateTime)Schedules.Tables["Schedule"].Rows[i + 1]["datetime"];
nextdate = nextdatetime.ToString("MM/dd/yyy");
}
string TeamA = Schedules.Tables["Schedule"].Rows[i]["teamA"].ToString();
string TeamB = Schedules.Tables["Schedule"].Rows[i]["teamB"].ToString();
//check to see if date is current
if (LastDate != ndate)
{
TableRow daterow = new TableRow();
TableCell datecell = new TableCell();
datecell.ColumnSpan = 7;
datecell.Controls.Add(new LiteralControl(ndate));
daterow.Cells.Add(datecell);
ScheduleTable.Rows.Add(daterow);
LastDate = ndate;
}
//print the games
if (currentdatetime != LastDateTime)
{
TableRow gamerow = new TableRow();
TableCell timecell = new TableCell();
timecell.Controls.Add(new LiteralControl(ntime));
gamerow.Cells.Add(timecell);
if (i + 1 != GameCount & ndate != nextdate)
{
ScheduleTable.Rows.Add(gamerow);
}
}//check to see if next game is part of the current row
else
{
TableCell gamecell = new TableCell();
gamecell.Controls.Add(new LiteralControl(TeamA + ".vs." + TeamB));
gamerow.Cells.Add(gamecell);
}
}
我还可以发布我目前在asp中的内容,如果这会有所帮助......你可以访问www.swgc.ca/volleyball/2011/schedules.asp看看我想要实现的目标。
由于
答案 0 :(得分:1)
将您的最后一位更改为:
TableRow gamerow = new TableRow();
if (currentdatetime != LastDateTime)
{
TableCell timecell = new TableCell();
timecell.Controls.Add(new LiteralControl(ntime));
gamerow.Cells.Add(timecell);
}//check to see if next game is part of the current row
else
{
TableCell gamecell = new TableCell();
gamecell.Controls.Add(new LiteralControl(TeamA + ".vs." + TeamB));
gamerow.Cells.Add(gamecell);
}
if (i + 1 != GameCount & ndate != nextdate)
{
ScheduleTable.Rows.Add(gamerow);
}
我强烈建议您查看网格视图和转发器/列表控件,因为这就是它们的用途。
答案 1 :(得分:0)
最简单的解决方案是将实例化拉出for循环。试试这个(未经测试的代码):
TableRow gamerow = new TableRow();
TableCell timecell = new TableCell();
TableCell gamecell = new TableCell();
TableRow daterow = new TableRow();
TableCell datecell = new TableCell();
for (int i = 0; i < GameCount; i++)
{
DateTime currentdatetime = (DateTime)Schedules.Tables["Schedule"].Rows[i]["datetime"];
string ndate = currentdatetime.ToString("MM/dd/yyy");
string ntime = currentdatetime.ToString("HH:mm");
string nextdate = currentdatetime.ToString("MM/dd/yyy");
if (i + 1 != GameCount)
{
DateTime nextdatetime = (DateTime)Schedules.Tables["Schedule"].Rows[i + 1]["datetime"];
nextdate = nextdatetime.ToString("MM/dd/yyy");
}
string TeamA = Schedules.Tables["Schedule"].Rows[i]["teamA"].ToString();
string TeamB = Schedules.Tables["Schedule"].Rows[i]["teamB"].ToString();
//check to see if date is current
if (LastDate != ndate)
{
daterow = new TableRow();
datecell = new TableCell();
datecell.ColumnSpan = 7;
datecell.Controls.Add(new LiteralControl(ndate));
daterow.Cells.Add(datecell);
ScheduleTable.Rows.Add(daterow);
LastDate = ndate;
}
//print the games
if (currentdatetime != LastDateTime)
{
gamerow = new TableRow();
timecell = new TableCell();
timecell.Controls.Add(new LiteralControl(ntime));
gamerow.Cells.Add(timecell);
if (i + 1 != GameCount & ndate != nextdate)
{
ScheduleTable.Rows.Add(gamerow);
}
}//check to see if next game is part of the current row
else
{
gamecell = new TableCell();
gamecell.Controls.Add(new LiteralControl(TeamA + ".vs." + TeamB));
gamerow.Cells.Add(gamecell);
}
这是针对您的问题的非优化答案。我觉得可能有更好的OO方式来实现你的目标,但不想回答你没有问过的问题。