为什么我的列表框没有填写?

时间:2013-12-04 10:48:18

标签: vb.net winforms csv listbox structure

我试图通过将每行的两个值分开并添加到结构来填充listbox .csv文件的内容。

Public Class Form1

Structure Members
    Dim Number As Integer
    Dim Name As String
End Structure

Dim Memberlist(30) As Members

Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim i As Integer = 0
    For Each line As String In IO.File.ReadAllLines("MembershipPhone.txt")
        Dim myData() = line.Split(","c)
        Memberlist(i).Name = myData(0)
        Memberlist(i).Number = myData(1)
        ListBox1.Items.Add(myData(0))
        i = i + 1
    Next
    ListBox1.SelectedItem = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

当我认为它可能属于VB.net时,我看到您将问题列为C#。我很乐意在C#中回答你的问题......

首先,我会使用标准的CSV阅读器。我发现它更容易,它可以解释一堆不同的格式。我使用标准的OleDbConnection和Microsoft JET引擎来读取数据库(How to read a CSV file into a .NET Datatable

获得Datatable后,您可以将其绑定到ListBox

//Load the data from the example above
DataTable myData = GetDataTableFromCsv(FilePath, true);

for (int i = 0; i < myData.Rows.Count; i++)
{
    //Grab all my data for this row
    string PersonID = (string)myData.Rows[i][0];
    string FirstName = (string)myData.Rows[i][1];
    string LastName = (string)myData.Rows[i][2];

    //Add what I want to the listbox
    ListBox1.Items.Add(FirstName);
}

//Select nothing from the listbox
ListBox1.SelectedIndex = -1;

希望有用!祝.NET好运,这很有趣,但我建议使用C#而不是VB: - )