none
Copy ListView items to array

    Question

  •  
    I'm trying to copy the items (item + 2 subitems) of a ListView to an array, using the ListView.ListViewItemCollection.CopyTo method, but can't figure out how to implement it in a procedure. Anyone who can give me a clue?

    thanks,
    Rich
    jeudi 7 août 2008 00:07

Réponses

Toutes les réponses

  •  

    object[] array = new object[listView1.Items.Count];

    listView1.Items.CopyTo(array,0);



    should do the trick :-)
    jeudi 7 août 2008 00:46
  • OsirisTerje said:

     

    object[] array = new object[listView1.Items.Count];

    listView1.Items.CopyTo(array,0)

     

    Thanks :-)

    However, in what format are the data stored in the array, and how do I read them out? There are three columns in the ListView so wouldn't you have to use at 3D array here?  

    Rich

     
    jeudi 7 août 2008 22:45
  • It's close, fix it like this:

          ListViewItem[] items = new ListViewItem[listView1.Items.Count];
          listView1.Items.CopyTo(items, 0);


    Hans Passant.
    vendredi 8 août 2008 00:00
    Modérateur
  • nobugz said:

    It's close, fix it like this:

          ListViewItem[] items = new ListViewItem[listView1.Items.Count];
          listView1.Items.CopyTo(items, 0);


    Thanks, but when I do a test read of the array to a textbox, like this:

    txtReadArray.Text = items[0];

    I get this error: Error 1 Cannot implicitly convert type 'System.Windows.Forms.ListViewItem' to 'string' 

    txtReadArray.Text = items[0].ToString;        

    gives another error: Error 1 Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method?

    Same with both suggestions. 

    Rich
     
    vendredi 8 août 2008 00:50
  • This is very elementary stuff, don't try to learn C# by trial and error.  An introductory book on C# will save you and us lots of time.  The fix:

      txtReadArray.Text = items[0].Text;

    and

      txtReadArray.Text = Items[0].ToString();


    Hans Passant.
    vendredi 8 août 2008 01:26
    Modérateur
  • nobugz said:

    This is very elementary stuff, don't try to learn C# by trial and error.  An introductory book on C# will save you and us lots of time.  The fix:

      txtReadArray.Text = items[0].Text;

    and

      txtReadArray.Text = Items[0].ToString();



    Oops - used autocomplete and simply forgot the "()". I'm already 3/4 though a good introductory book plus several tutorials and other stuff, and have used ToString(); hundreds of times without any problems, so this was simply a plain "late night" bummer. Sorry... :-)

    BTW, in case anyone should be interested, the two solutions generate a different output:

    ToString(); = ListViewItem: {itemdata}
    Text; = itemdata


    Thanks!
    Rich

    vendredi 8 août 2008 15:43