当前位置:编程学习 > C#/ASP.NET >>

如何用Listview.Items.AddRange读取文本中数据,求好心人..在线等!!

文本中有几万条数据, add方法添加太慢,听说AddRange可以提高效率,请问如何用Listview.Items.AddRange,

读取str = sr.ReadLine()的数据,请给出您修改的代码 感谢

 部分代码
  openFileDialog1.ShowDialog();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                //打开文件对话框中选择的文件名
                 
                //创建从字符串进行读取的StringReader对象
               
                fname = openFileDialog1.FileName;
                
                StreamReader sr = File.OpenText(fname);
                    
                lvInceptNum.BeginUpdate();
          
                    ListViewItem[] shu = new ListView[j];
                  
                      sr = File.OpenText(fname);
  
                      while ((str = sr.ReadLine()) != null)                                           

                  {    Listview.Items.AddRange(str);}

                lvInceptNum.EndUpdate();
            } --------------------编程问答--------------------
            openFileDialog1.ShowDialog();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                //打开文件对话框中选择的文件名

                //创建从字符串进行读取的StringReader对象

                fname = openFileDialog1.FileName;

                String[] allLine = File.ReadAllLines(fname);
                lvInceptNum.BeginUpdate();

                ListViewItem[] items = new ListViewItem[allLine.Length];
                Int32 length = allLine.Length;
                for (Int32 index = 0; index < length; index++)
                {
                    items[index] = new ListViewItem(allLine[index]);
                }
                ListView.Items.AddRange(items);

                lvInceptNum.EndUpdate();
            }

你试下这段代码,应该会快很多。 --------------------编程问答--------------------
引用 1 楼 syuesw 的回复:
C# code
            openFileDialog1.ShowDialog();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                //打开文件对话框中选择的文件名

                //创建从字符串进行读取的……


支持。 --------------------编程问答-------------------- 建议楼主用view --------------------编程问答--------------------
补充:.NET技术 ,  其他语言
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,