Issue with ListView Control

This is a discussion on Issue with ListView Control within the C# Programming forums, part of the General Programming Boards category; This is driving me crazy. I have a listview control on form in VC# 2008 Express. There are three colums ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    Issue with ListView Control

    This is driving me crazy. I have a listview control on form in VC# 2008 Express.
    There are three colums set in its properties and its in details mode. I'm trying to add rows of data in at run time but any time I try and access the list Items (either to Add or Clear) I'm told that I have a NullReferenceException and that "Object reference not set to an instance of an object."

    What am I doing wrong?

    Code:
    ListViewItem item = new ListViewItem("NewItem");
    item.SubItems.AddRange( new string[] { "SubItem1", "SubItem2" });
    
    lvProcesses.Items.Add("item");

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    It's easier to have a data binding source associated with the list view, then set the data source to a bindingList that contains your element type, and the DataMember property set to the data the BindingList Holds. Then when you add your stuff to the bindinglist, it automatically updates the listview control.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    Unfortunatly that's not really an option here, I'm trying to populate the ListView with the list of processes currently running on a target system. So it has to be done at runtime.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    What is lvProcesses?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    lvProcesses is just the ListView Control itself

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    it seems to me you are trying to add string "item" instead of object ListViewItem item
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    I've had it both ways around, neither works. The string version is a valid version of the overloaded method, but the issue seems to be that the ListViewItemContainter Items (that's lvProcesses.Itemsin this case) hasn't been instanciated, however you can't individually instaciate it as its read only.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    1) You forgot to (manually) create a System.Windows.Forms.ListView object and assign it to lvProcesses
    2) You are trying to add the item before the call to InitializeComponent (in your form class)

    1) is if you create your controls manually, 2) is if you're using the visual designer
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    Talking

    Quote Originally Posted by Magos View Post
    1) You forgot to (manually) create a System.Windows.Forms.ListView object and assign it to lvProcesses
    2) You are trying to add the item before the call to InitializeComponent (in your form class)

    1) is if you create your controls manually, 2) is if you're using the visual designer
    Ahaha!! Fantastic thanks, I'd put it in the Initial method of the form to test things but before InitalizeComponents had been called as a test. That's solved it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Listview control
    By Mavix in forum C# Programming
    Replies: 6
    Last Post: 12-21-2007, 12:53 PM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  3. A few questions on ListView Control...
    By Devil Panther in forum Windows Programming
    Replies: 0
    Last Post: 09-05-2003, 02:33 PM
  4. Edit control on listview
    By knutso in forum Windows Programming
    Replies: 5
    Last Post: 08-10-2003, 09:11 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21