如何将文件中的数据格式化为列表视图?

时间:2017-03-13 11:46:35

标签: .net vb.net listview

我有以下代码,我的图片显示错误。我无法格式化数据,因此它们各自都在各自的列中。

    Call loadCustomerListView(obj_CustDetails.getFileName, lstViewAllCustomers)
    obj_CustDetails = New filehandling("CustomerDetails.txt")
    StockArray = obj_CustDetails.GetAllLines()


    inputSug = New InputSuggestion(ListData, StockArray, True, 76, 87)

    StockData = obj_CustDetails.GetAllLines
    Dim StockQuan() As String = obj_Stock.GetAllLines

    For i = 0 To UBound(StockQuan)

    Next

在列表视图中,您可以看到错误是数据格式不正确

Image 1

1 个答案:

答案 0 :(得分:0)

您必须为每个属性创建一个Class以匹配您的数据。 当您提取数据时,每个属性必须容纳每个值,然后它将应用于每个列。 稍后,您将创建一个Class对象列表,并将其加载到网格上。

Private Class MyClass
    Public Property ID() As Integer
        Get
            Return m_ID
        End Get
        Set
            m_ID = Value
        End Set
    End Property
    Private m_ID As Integer
    Public Property FirstName() As String
        Get
            Return m_FirstName
        End Get
        Set
            m_FirstName = Value
        End Set
    End Property
    Private m_FirstName As String
    Public Property SecondName() As String
        Get
            Return m_SecondName
        End Get
        Set
            m_SecondName = Value
        End Set
    End Property
    Private m_SecondName As String
End Class

Private Sub PopulateGrid()
    Dim test1 As New TestObject() With { _
          .ID = "1231", _
         .FirstName = "John 1", _
         .Text = "dfasfsdf1" _
    }
    Dim test2 As New TestObject() With { _
           .ID = "1232", _
         .SecondName = "FASDFASDF 2", _
         .Text = "FASDFADF 2" _
    }
    Dim list As New List(Of MyClass)()
    list.Add(test1)
    list.Add(test2)

    dataGridView1.DataSource = list
End Sub