使用结构化数组填充DataGridView时出现问题

时间:2019-04-19 22:10:14

标签: vb.net data-structures datagridview

运行代码时,使用csv日志文件中的数据填充数组,DataGridView填充空白行。

我尝试仅使用二维数组,得到相同的结果。我怀疑我需要以某种方式“映射”数据……也许?

这是我目前正在尝试的方法:
第一部分在Module ...中,子代码在表单代码中

Public Structure DataBlock

    Public Data As String()

    Public Property xData As String()
        Get
            Return Data
        End Get
        Set(ByVal value As String())
            Data = value
        End Set
    End Property

End Structure

Public DataBlocks(1) As DataBlock
' end of module code
' start of form code
Public Sub test(path)
    Dim i As Integer
    LogData = IO.File.ReadAllLines(path)
    ReDim DataBlocks(UBound(LogData))
    For i = 0 To UBound(LogData)
        DataBlocks(i) = New DataBlock With {.xData = Split(LogData(i), ",")}
    Next
    DataGridView1.DataSource = DataBlocks

End Sub

我期望填充该表,我看到了其他有效的示例,但这些示例未在结构中使用结构化数组。我可以将数组分解为8个部分(这是文件的列数),但是我还不愿意割裂。

1 个答案:

答案 0 :(得分:0)

好吧,虽然有点笨拙,但是可以用。我将结构更改为一个类。看来DataGridView无法将数组作为列类型处理,所以我将数组更改为显示字符串。

Public Class DataBlock
    Private Data As String()
    Public ReadOnly Property xDataOut As String
        Get
            Return String.Join(", ", Data)
        End Get
    End Property
    Public WriteOnly Property xDataIn As String()
        Set(value As String())
            Data = value
        End Set
    End Property
    Public Sub New(sArray As String())
        xDataIn = sArray
    End Sub
End Class

Public DataBlocks As New List(Of DataBlock)

Public Sub test(path As String)
    Dim Lines = IO.File.ReadAllLines(path)
    For Each line In Lines
        DataBlocks.Add(New DataBlock(line.Split(","c)))
    Next
    DataGridView1.DataSource = DataBlocks
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    test("LogData.txt")
End Sub