Filling up a listview dynamically with file contents

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • VM

    Filling up a listview dynamically with file contents

    How can I fill up a listview with text file contents?
    My listview has two columns and the first column fills up with a while loop:

    while (myString != null)
    {
    myString = sr.Readline();
    listView1.Items .Add (myString);
    }

    Is there a way I can fill up the second column this easily? Thw way I'm currently doing it is using the subitems property to add it. Unfortunately,
    I'm using a counter to tell it which line to fill up and adding to the second column is directly related to the first item (eg. I can't add a subitem on the 2nd column unlee I have added one in the 1st column).

    Also, the MSDN sample doesn't work correctly. In the following code from MSDN, if I copy/paste it'll work perfectly. But if I comment the first line so it uses the listView I created in the designer (instead of creating a new one), it doesn't have the same effect.

    // ListView listView1 = new ListView(); /* Commented so it uses existing listView1 */
    listView1.Bound s = new Rectangle(new Point(10,10), new Size(300,200));
    listView1.View = View.Details;
    ListViewItem item1 = new ListViewItem("i tem1",0);
    item1.Checked = true;
    item1.SubItems. Add("1");
    item1.SubItems. Add("2");
    item1.SubItems. Add("3");
    ListViewItem item2 = new ListViewItem("i tem2",1);
    item2.SubItems. Add("4");
    item2.SubItems. Add("5");
    item2.SubItems. Add("6");
    listView1.Colum ns.Add("Item Column", -2, HorizontalAlign ment.Left);
    listView1.Colum ns.Add("Column 2", -2, HorizontalAlign ment.Left);
    listView1.Items .AddRange(new ListViewItem[]{item1,item2});
    this.Controls.A dd(listView1);

    Thanks.
Working...