C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-28-2008, 02:53 PM   #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");
elaverick is offline   Reply With Quote
Old 04-28-2008, 04:02 PM   #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   Reply With Quote
Old 04-29-2008, 12:06 AM   #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   Reply With Quote
Old 04-29-2008, 12:11 AM   #4
CSharpener
 
vart's Avatar
 
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   Reply With Quote
Old 04-29-2008, 12:12 AM   #5
Registered User
 
Join Date: Apr 2008
Posts: 5
lvProcesses is just the ListView Control itself
elaverick is offline   Reply With Quote
Old 04-29-2008, 12:23 AM   #6
CSharpener
 
vart's Avatar
 
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   Reply With Quote
Old 04-29-2008, 12:26 AM   #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   Reply With Quote
Old 04-29-2008, 01:20 AM   #8
Confused
 
Magos's Avatar
 
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   Reply With Quote
Old 04-29-2008, 03:42 AM   #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
elaverick is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:56 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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