Addrange Listview
-
I am trying to add a range of arrays to al istview from 3 files. My arrays are decalred as:
string[] sn = File.ReadAllLines("serverList.txt");
string[] sl = File.ReadAllLines("serverLocation.txt");
string[] st = File.ReadAllLines("serverType.txt");But when I use:
ListViewItem lvi = new ListViewItem();
lvi = lv.Items.AddRange(sn);
lvi.SubItems.AddRange(sl);
lvi.SubItems.AddRange(st);Its tells me the addrange taked invalid arguments, why?
-
I am trying to add a range of arrays to al istview from 3 files. My arrays are decalred as:
string[] sn = File.ReadAllLines("serverList.txt");
string[] sl = File.ReadAllLines("serverLocation.txt");
string[] st = File.ReadAllLines("serverType.txt");But when I use:
ListViewItem lvi = new ListViewItem();
lvi = lv.Items.AddRange(sn);
lvi.SubItems.AddRange(sl);
lvi.SubItems.AddRange(st);Its tells me the addrange taked invalid arguments, why?
The AddRange[^] method has two overloads, one takes in a ListViewItemCollection object and the other takes in a Generic Array of ListViewItem objects. You are trying to pass a string array which cannot be implicity convered to either of the required objects stated above. To acheive what you are trying to do, you must iterate through the string arrays, build a List<ListViewItem> object and pass it to the AddRange method.
-
I am trying to add a range of arrays to al istview from 3 files. My arrays are decalred as:
string[] sn = File.ReadAllLines("serverList.txt");
string[] sl = File.ReadAllLines("serverLocation.txt");
string[] st = File.ReadAllLines("serverType.txt");But when I use:
ListViewItem lvi = new ListViewItem();
lvi = lv.Items.AddRange(sn);
lvi.SubItems.AddRange(sl);
lvi.SubItems.AddRange(st);Its tells me the addrange taked invalid arguments, why?
ListView.Items.Add() is horridly slow. So you're onto the right idea when adding a lot of things This may help you
// build your ArrayList. I give foreach loop as an example, because this is the most common complaint about ListView being slow. ArrayList al = new ArrayList(); foreach () { ListViewItem lvi = new ListViewItem(); lvi.Text = "This is a ListView Item"; lvi.SubItems.Add("This is for 2nd column text when ListView.View == Details"); lvi.SubItems.Add("This is for 3rd column text when ListView.View == Details"); //etc etc with more columns if you want al.Add(lvi); } Type t = typeof(ListViewItem); // I haven't tested the performance enhancement, but I believe suspending the layout will speed up things a little more this.\_listView.SuspendLayout(); this.\_listView.Items.AddRange(al.ToArray(t) as ListViewItem\[\]); this.\_listView.ResumeLayout();
That's the general idea behind AddRange(). It speeds things up when adding ListViewItems. Depending on what you want your output to look like, take the above and modify it to build up some ArrayLists from your string[] fields
-
I am trying to add a range of arrays to al istview from 3 files. My arrays are decalred as:
string[] sn = File.ReadAllLines("serverList.txt");
string[] sl = File.ReadAllLines("serverLocation.txt");
string[] st = File.ReadAllLines("serverType.txt");But when I use:
ListViewItem lvi = new ListViewItem();
lvi = lv.Items.AddRange(sn);
lvi.SubItems.AddRange(sl);
lvi.SubItems.AddRange(st);Its tells me the addrange taked invalid arguments, why?
There are some simple ways to add a ListViewItem: (this example assumes a ListView with four columns defined, and View = Details):
listView1.Items.Add(new ListViewItem(new[] { "1", "2", "3", "4" }));
My guess is there's some neat trick involving Linq where you could take the three lists the OP describes and get them into usable shape (but that's beyond my weirding-powers) :). best, Bill
"Is it a fact - or have I dreamt it - that, by means of electricity, the world of matter has become a great nerve, vibrating thousands of miles in a breathless point of time? Rather, the round globe is a vast head, a brain, instinct with intelligence!" - Nathanial Hawthorne, House of the Seven Gables