2012-04-17 13 views
0

データベースからスケジュールを印刷してループを使用しています。私はaspでこれを行っているが、私はC#asp.netに変わり、問題を抱えている。裁判所| |法廷|裁判所の数に基づいて裁判所C#ASP.net変数がスコープ内でスコープから外れる

それはゲームを印刷し

まず私は

時間はスケジュールヘッダを印刷します。 次のレコードの現在の日付は、テーブル行全体にわたって日付を出力する最後の日付とは異なります。 それから、現在のレコードの時刻と最後の時刻が同じであるかどうかを確認し、時刻を出力していない場合はゲームレコードを表示します。

私の問題は、私が時間外にstatmentを使用しようとしたときに、その時点で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); 
     } 


    } 

私はまた、あなたが、私が達成しようとしているかを確認するためにwww.swgc.ca/volleyball/2011/schedules.aspに行くことができます...それが役立つならば、私は現在、ASPで持っているもの投稿することができます。

おかげ

+0

このコードは、読みやすくするために省略してください。 – adt

答えて

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

そして、これは彼らが何であるかであるように私は強く、gridviewsおよびリピータ/リストコントロールを見てお勧めします。

+0

グリッドビューでは、新しいレコードの各レコードを表示する必要はありませんか? – trowse

+0

Gridviewはそうですが、リスト/リピータコントロールは柔軟性を提供し、先進的なものとして保守性が向上します。 – Paddy

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

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

+0

URG !!私はそれを試み、それがうまくいかなかったと仮定しました...しかし、誰か他の人がそれを言ったとき、 "別の部分に何か問題があるかもしれないと思いました。どのようにあなたはこれを最適化すると思いますか? – trowse

+0

私は、表示する方法と同様に印刷するSQLを取得することから始めます:SELECT [datetime]、STUFF((SELECT '、' + [teamA] + ' ( ''))、1,2、 '')AS NameValues スケジュール結果から GROUP BY datetime –

+0

re:http:// [チームB] stackoverflow.com/questions/273238/how-to-use-group-by-to-concatenate-strings-in-sql-server –

関連する問題