![]() |
| | #1 |
| Registered User Join Date: Apr 2008
Posts: 5
| Issue with ListView Control 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");
|
| elaverick is offline | |
| | #2 |
| Ethernal Noob Join Date: Nov 2001
Posts: 1,891
| 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. |
| indigo0086 is offline | |
| | #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. |
| elaverick is offline | |
| | #4 |
| CSharpener Join Date: Oct 2006
Posts: 5,336
| What is lvProcesses?
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #5 |
| Registered User Join Date: Apr 2008
Posts: 5
| lvProcesses is just the ListView Control itself |
| elaverick is offline | |
| | #6 |
| CSharpener Join Date: Oct 2006
Posts: 5,336
| it seems to me you are trying to add string "item" instead of object ListViewItem item
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #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. |
| elaverick is offline | |
| | #8 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,125
| 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. |
| Magos is offline | |
| | #9 | |
| Registered User Join Date: Apr 2008
Posts: 5
| Quote:
| |
| elaverick is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Listview control | Mavix | C# Programming | 6 | 12-21-2007 01:53 PM |
| Button handler | Nephiroth | Windows Programming | 8 | 03-12-2006 06:23 AM |
| A few questions on ListView Control... | Devil Panther | Windows Programming | 0 | 09-05-2003 02:33 PM |
| Edit control on listview | knutso | Windows Programming | 5 | 08-10-2003 09:11 PM |
| Tab Controls - API | -KEN- | Windows Programming | 7 | 06-02-2002 09:44 AM |