0

我正在尝试通过使用索引来遍历结构。除了反射之外,有没有一种简单的方法可以做到这一点?

代码示例;

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

假设我尝试使用当前索引值为每个单元格分配不同的颜色,例如 Drawing.Color(j)

4

1 回答 1

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 添加颜色的 hack - 不确定这是合法代码,我只是将它用作实用程序。

于 2013-09-24T16:15:45.820 回答