0

データベースからスケジュールを印刷し、ループを使用しています。私はaspでこれを行いましたが、c#asp.netに変更して問題を抱えています。

まず、スケジュール ヘッダーを印刷します。

時間|裁判所|裁判所|裁判所

コートの数に基づいて、ゲームを印刷します。次に、現在のレコードの日付は、表の行全体に日付を印刷する最後の日付とは異なります。次に、現在のレコードの時間が前回と同じかどうかを確認し、そうでない場合は時間を出力し、ゲームの記録が同じ場合はゲームの記録を出力します。

私の問題は、時間 if ステートメントで 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 にあるものを投稿することもできます。

ありがとう

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

これはあなたの質問に対する最適化されていない答えです。私はおそらくあなたの目標を達成するためのより良いOOの方法があるように感じますが、あなたが尋ねなかった質問に答えたくありませんでした。

于 2012-04-17T12:00:02.077 に答える