2013-09-24 28 views
0

我想通過使用索引遍歷結構。除了反思之外,有沒有簡單的方法呢?遍歷VB中索引的結構

代碼示例;

For j As Integer = 0 To usedRange.ColumnCount - 1 
      ws.Cells(0, j).FillColor = Drawing.Color.DarkTurquoise 
Next 

比方說,我嘗試使用當前索引值,如Drawing.Color(J)

+1

這不是'數據結構',它是Excel工作表中的一組單元格。你會重新回答這個問題,因爲你不清楚你在問什麼。 – Robin

+0

同上。 ws代表什麼?看起來你可能在單元格中迭代得很好(取決於什麼是ws)。 – rheitzman

回答

0

如果您正在尋找枚舉顏色分配不同的顏色,以每個單元:

' Get all the values from the KnownColor enumeration. 
    Dim colorsArray As System.Array = [Enum].GetValues(GetType(KnownColor)) 
    Dim allColors(colorsArray.Length) As KnownColor 
    Array.Copy(colorsArray, allColors, colorsArray.Length - 1) 
    With dgvColors 
     .ReadOnly = True 
     .Columns.Add("ColorName", "ColorName") 
     For i = 0 To allColors.Length - 1 
      .Rows.Add(allColors(i).ToString) 
      .Rows(i).Cells(0).Style.BackColor = Color.FromKnownColor(allColors(i)) 
     Next 
     .Columns("ColorName").Width = 500 
     .Rows.RemoveAt(.Rows.Count - 2) ' delete - bug in arrays 
     .Rows.RemoveAt(.Rows.Count - 2) ' delete - bug in arrays 
     .ClearSelection() 
    End With 

這是向DataGridView添加顏色的黑客攻擊 - 不確定這是否是合法代碼,我只是將它用作實用程序。