Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Addrange Listview

Addrange Listview

Scheduled Pinned Locked Moved C#
question
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Herboren
    wrote on last edited by
    #1

    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?

    L C B 3 Replies Last reply
    0
    • H Herboren

      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?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • H Herboren

        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?

        C Offline
        C Offline
        Columbus MCSD
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • H Herboren

          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?

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups