Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用数组填充ListView_C#_Arrays_Performance_Listview - Fatal编程技术网

C# 使用数组填充ListView

C# 使用数组填充ListView,c#,arrays,performance,listview,C#,Arrays,Performance,Listview,我正在尝试将大量数据加载到listView。下面的代码正在运行,但我想创建一个列表视图项数组,然后将该数组添加到我的列表视图中。有人能告诉我怎么做吗 using (var csv = new CsvReader(new StreamReader(openFileDialog1.FileName), true)) { int fieldCount = csv.FieldCount; string[] headers = csv.GetFieldHeaders(); int

我正在尝试将大量数据加载到listView。下面的代码正在运行,但我想创建一个列表视图项数组,然后将该数组添加到我的列表视图中。有人能告诉我怎么做吗

using (var csv = new CsvReader(new StreamReader(openFileDialog1.FileName), true))
{
    int fieldCount = csv.FieldCount;
    string[] headers = csv.GetFieldHeaders();
    int i = 0;
    while (csv.ReadNextRecord())
    {
        this.listView1.Items.Add(
            new ListViewItem(new[] { csv[0], csv[1], csv[2], csv[3], csv[4] })
        );
    }
}

除非您事先知道csv中有多少行,否则最简单的方法可能是使用列表:

List<ListViewItem> items = new List<ListViewItem>();
while (csv.ReadNextRecord())
    items.Add(new ListViewItem(new[] { csv[0], csv[1], csv[2], csv[3], csv[4] }));
ListViewItem[] array = items.ToArray();
this.listView1.Items.AddRange(array);
List items=newlist();
而(csv.ReadNextRecord())
添加(新ListViewItem(新[]{csv[0],csv[1],csv[2],csv[3],csv[4]});
ListViewItem[]数组=items.ToArray();
this.listView1.Items.AddRange(数组);
不过,你已经拥有的方式应该足够了。如果您关心渲染速度,可以在
listView1.BeginUpdate()和
listView1.EndUpdate()调用中包装代码以加快渲染速度

您可以在此处了解更多信息:

什么是“问题”?