0

我正在从数据库打印计划并使用循环。我已经在 asp 中完成了此操作,但我正在更改为 c# asp.net 并且遇到了麻烦。

首先我打印时间表标题

时间|法庭|法庭|法庭

根据球场的数量,它会打印游戏。接下来 ff 当前记录日期与最后一个日期不同,它将在整个表格行上打印日期。然后检查当前记录的时间是否与上一次相同,如果不是,则打印时间,然后是游戏记录,如果是,则仅打印游戏记录。

我的问题是我在 if 语句中声明了 TableRow,所以当我尝试在另一个语句中使用它时,它超出了范围。如果我将 tablerow 放在 if 语句之外,它就不能正确打印。

这就是我所拥有的。

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 看看我想要完成什么。

谢谢

4

2 回答 2

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

我强烈建议查看网格视图和中继器/列表控件,因为这就是它们的用途。

于 2012-04-17T11:48:51.060 回答
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);
        }

这是您问题的非优化答案。我觉得可能有更好的面向对象方法来实现您的目标,但不想回答您没有提出的问题。

于 2012-04-17T12:00:02.077 回答