将CSV文件加载到2D数组中,然后通过列表框显示(C#)

将CSV文件加载到2D数组中,然后通过列表框显示(C#)

问题描述:

最终目标是将csv文件加载到二维数组中,然后将这些数据显示到我创建的列表框中。下面的代码应该加载csv文件,但我不知道我现在如何将我创建的2D数组放入列表框中。



我尝试过:



The end goal is to load a csv file into a two-dimensional array then display those data into the listbox I created. The code below should load the csv file but I dont know how I can now get the 2D array I created into the listbox.

What I have tried:

string filePath = "data.txt"; // CSV File
int count = 0;
string[,] list = new string[6, 4];


private void App_Load(object sender, EventArgs e)
{

        FileStream fstrm = new FileStream(path, FileMode.Open,          FileAccess.Read);
        StreamReader sread = new StreamReader(fstrm);

        while (!sread.EndOfStream)
        {
            string line = sr.ReadLine();
            string[] parts = line.Split(',');

            for (int i = 0; i < parts.Length; i++)
            {
                list[count, i] = parts[i];
            }
            count++;


        }

列表框只包含一列,所以它不会采用2D阵列。但是,请参阅 ListBox.Items属性(系统.Windows.Forms) [ ^ ]其他可能性。
A listbox contains only a single column, so it will not take a 2D array as it stands. However, see ListBox.Items Property (System.Windows.Forms)[^] for other possibilities.