2016-07-05 76 views
0

我目前正在循環訪問一個數組,以便使用visual basic在asp.net中填充表。我想填充數組中前9個項目的第一行,然後從計數器的當前位置填充下一行。我只用了2天就可以使用vb,我的當前循環用索引0-9填充每一行,但最好是從10-18開始填充下一行,依此類推。動態循環訪問數組

<div class="col-md-10 col-md-offset-1"> 

    <h3 style="text-align:center; padding: 0px;">List of Alerts for Today</h3> 
    <table id="myTable" class="tablesorter" style="width:100%"> 
     <thead class="finger_hover"> 
      <tr> 
       <th>Sensor</th> 
       <th>Date Time</th> 
       <th>Source IP</th> 
       <th>Source Port</th> 
       <th>Destination IP</th> 
       <th>Destination Port</th> 
       <th>Protocol</th> 
       <th>Signature</th> 
       <th>Signature Class</th> 
       <th>Count</th> 

      </tr> 
     </thead> 

     <tbody> 

      @{ 

       int i; 
       for (i = 0; i < alertForTheDay.Count; i++) 
       { 


        @:<tr> 
         @: 
         <td>@alertForTheDay[i]</td> 
           @: 
           <td>@alertForTheDay[i + 1]</td> 
           @: 
           <td>@alertForTheDay[i + 2]</td> 
           @: 
           <td>@alertForTheDay[i + 3]</td> 
           @: 
           <td>@alertForTheDay[i + 4]</td> 
           @: 
           <td>@alertForTheDay[i + 5]</td> 
           @: 
           <td>@alertForTheDay[i + 6]</td> 
           @: 
           <td>@alertForTheDay[i + 7]</td> 
           @: 
           <td>@alertForTheDay[i + 8]</td> 
           @: 
           <td>@alertForTheDay[i + 9]</td> 


           @:</tr> 

        i = i + 9; 

       } 


      } 

     </tbody> 

    </table> 

    </div> 

此代碼填充下一行與下一集合的數組中的項:在C#我已使用如下所示的代碼來實現這一點。下面是VB的代碼:

<div class="col-md-10 col-md-offset-1"> 

    <h3 style="text-align:center; padding: 0px;">List of Alerts for Today</h3> 
    <table id="myTable" class="tablesorter" style="width:100%"> 
     <thead class="finger_hover"> 
      <tr> 
       <th>Sensor</th> 
       <th>Date Time</th> 
       <th>Source IP</th> 
       <th>Source Port</th> 
       <th>Destination IP</th> 
       <th>Destination Port</th> 
       <th>Protocol</th> 
       <th>Signature</th> 
       <th>Signature Class</th> 
       <th>Count</th> 

      </tr> 
     </thead> 

     <tbody> 

      @code 
       Dim increment As Integer = 9 
       Dim i As Integer 
       For i = 0 To alertForTheDay.Count Step i + 1 

        @:<tr> 

         @:<td>@alertForTheDay(0)</td> 
         @:<td>@alertForTheDay(1)</td> 
         @:<td>@alertForTheDay(2)</td> 
         @:<td>@alertForTheDay(3)</td> 
         @:<td>@alertForTheDay(4)</td> 
         @:<td>@alertForTheDay(5)</td> 
         @:<td>@alertForTheDay(6)</td> 
         @:<td>@alertForTheDay(7)</td> 
         @:<td>@alertForTheDay(8)</td> 
         @:<td>@alertForTheDay(9)</td> 

         @:</tr> 

        i = i + 9 

       Next 

      End code 

     </tbody> 

    </table> 

</div> 

VB代碼填充細胞與精確索引並且即使當我通過代碼和數量的變化的步驟,所述細胞總是與所引用的精確值填充。我相信有這樣一個簡單的方法。請任何幫助表示讚賞。由於

回答

1

只要改變你的循環由10增加和使用循環的索引從數組中檢索你的價值觀

  For i = 0 To alertForTheDay.Count -1 Step 10 

       @:<tr> 

        @:<td>@alertForTheDay(i)</td> 
        @:<td>@alertForTheDay(i+1)</td> 
        @:<td>@alertForTheDay(i+2)</td> 
        @:<td>@alertForTheDay(i+3)</td> 
        @:<td>@alertForTheDay(i+4)</td> 
        @:<td>@alertForTheDay(i+5)</td> 
        @:<td>@alertForTheDay(i+6)</td> 
        @:<td>@alertForTheDay(i+7)</td> 
        @:<td>@alertForTheDay(i+8)</td> 
        @:<td>@alertForTheDay(i+9)</td> 

        @:</tr> 

      Next 

當然,這假設你alertForTheDay包含多個元素的總的倍數10

我所固定的:

  • 循環退出condion應該比元素的計數1以下,因爲陣列在索引0處開始
  • 遞增步數爲10個元素(行中所需元素的數量爲
  • 使用i的值作爲基數從數組中提取元素,然後遞增該值以達到10個塊中的每個元素
  • 刪除我的最後的增量(在for循環的第10步讓這個沒用)
+0

那正是我想要的。像魅力一樣工作。感謝您的幫助,我真的很感激:) – BuzzLightYear