Click here to Skip to main content
16,018,797 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Hi,

I'm a beginner and just need some help solving this problem.

I think its simple but have tried to figure it out but im getting no where.
I currently have four text boxes to which I add data then click the add to add this to the listview.

I am able to add the first time but then can not add again also this does not add data from the first text box to the first column but leaves the first column out and starts from the second column.


below is the code
C#
ListViewItem item1 = new ListViewItem();
// add items.
item1.SubItems.Add((Convert.ToString(StudentHouses.HouseNo)));
item1.SubItems.Add((StudentHouses.AddressLine1));
item1.SubItems.Add((StudentHouses.AddressLine2));
item1.SubItems.Add((StudentHouses.AddressLine3));
//Add the items to the ListView.
listView1.Items.Add( item1 );
this.Controls.Add(listView1);


Any help would be appreciated.
thankyou

[edit]Subject: Don't SHOUT. All uppercase is considered SHOUTING on the internet, and rude - OriginalGriff[/edit]
Posted
Updated 13-Mar-11 11:24am
v4
Comments
Dalek Dave 13-Mar-11 17:06pm    
Edited for Readability.
Sergey Alexandrovich Kryukov 13-Mar-11 19:26pm    
You attempt would be correct, but ListViewControl does not exactly what do you think.
This is because this control is ugly in certain respect.
However, if your simply looked at help you would easily find the resolution yourself.
--SA
BEGINNERINEED 16-Mar-11 15:20pm    
ive looked for help where possible by exploring various methods but have struggled and i thought the best thing would be to ask the experts which is very much appreciated, but still im struggling to add multiple times to the listview and it only adds the first time, why would this be?
Sergey Alexandrovich Kryukov 16-Mar-11 15:31pm    
You code does not show this problem. Did you add columns properly? Do you change the index of sub-item properly. Usual mistake is repeating same operation several times. Use proper loop.
--SA
BEGINNERINEED 16-Mar-11 17:12pm    
i think i have added the columns properyly but yes i have not at anytime changed the index as ive used no loop to iterate through the items being added

Not sure why you wouldn't be able to add again unless your trying to add a duplicate list view item, try calling listView1.Items.Clear();

With regards to the starting at second column it is confusing I found also. The item text appears in the first column and sub items start at column two. This means your code should be more like: (included other adaptations as well)

C#
//Clear listView1 items.
listView1.Items.Clear();

//Create item1
ListViewItem item1 = new ListViewItem();

//Set Text which represents first column
item1.Text = Convert.ToString(StudentHouses.HouseNo);

//Add sub items starting with second column
item1.SubItems.Add((StudentHouses.AddressLine1));
item1.SubItems.Add((StudentHouses.AddressLine2));
item1.SubItems.Add((StudentHouses.AddressLine3));

//Add the items to the ListView.
listView1.Items.Add( item1 );

//Add listView1 control if it has not already been added
if(!this.Controls.Contains(listView1))
{
    this.Controls.Add(listView1);
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Mar-11 19:24pm    
Now it's correct, my 5.
Also, a shorter way is ListViewItem item1 = new ListViewItem(StudentHouses.HouseNo.ToString());
(Text property assignment would be redundant.)
--SA
Ed Nutting 14-Mar-11 3:19am    
'Not' or 'Now'? :P And yes but I wanted to demonstrate the concept better so used text property instead :).
Sergey Alexandrovich Kryukov 14-Mar-11 3:35am    
Thanks for correcting me.
--SA
BEGINNERINEED 16-Mar-11 15:07pm    
thanks for the demo, i have it adding to the listview starting from column1 now, however am still not able to add the second time, do you think i am better of using a loop or is it something else that im doing wrong as it only adds the first time to the listview
Sergey Alexandrovich Kryukov 16-Mar-11 15:35pm    
Rather, you need to separate model and UI presentation. Have a distinct model in pure data, no UI and separate mapping between them, never repeating a single line of code as you do in your SubItems.Add. Sure, a cycle.
Look, check from time to time my profile. I'm getting ready the article, the demo uses a tree view iterating through enumeration type. In the mean while please see my very last article on enumeration and locate "Enumeration Types do not Enumerate! Working around .NET and Language Limitations", to get some ideas (there is not list view there, but will be in my future new article, really soon).
--SA
Disclaimer:
This code posted "as is", on request by OP.
It is working and very well-tested but I cannot take responsibility for its value for the any purposes.
This is a code fragment from the demo application for my unpublished article "Dynamic Method Dispatcher" and is not related to the main topic of my article.

The only reason I post it in advance is that OP asked for it.

The purpose if this post is to give an idea of code design based on separation of data model from UI, not for explaining each declaration in any level of detail.
C#
using System;
using System.Windows.Forms;
using Time = System.DateTime;

//...

ListViewItem ShowCommon(
        Time time, ListView listView,
        MessageInfo info,
        string customData, bool autoResize) {
    ListViewItem item =
        new ListViewItem(TestRenderers[(int)CommonColumn.Time](ref time, ref info, customData, true));
    for (int index = 0; index < listView.Columns.Count - 1; index++)
        item.SubItems.Add(new ListViewItem.ListViewSubItem());
    for (
        CommonColumn index = (CommonColumn)1;
        index < (CommonColumn)(Enum.GetValues(typeof(CommonColumn)).Length - 1);
        index++) {
            item.SubItems[(int)index].Text =
                TestRenderers[(int)index](ref time, ref info, customData, true);
    } //loop
    item.SubItems[listView.Columns.Count - 1].Text = customData;
    listView.Items.Add(item);
    if (autoResize)
        AutoResize(listView);
    listView.EnsureVisible(item.Index);
    ShowStatus(ref time, ref info, customData);
    return item;
} //ShowCommon


This code represents general purpose presentation of a Windows message. It transparently passes the instance of the ListViewItem for adding data specific to a specialized ranges of message IDs, like keyboard, mouse, status, etc.

Pay attention that no immediate constants are used (except 0, 1 and null). All data is extracted from some enumeration type definitions and arrays based on those definitions.

On enumerations, please see my series of three articles:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^]
Human-readable Enumeration Meta-data[^]
Enumeration-based Command Line Utility[^].

The most relevant is the first article in the series, and a bit of the second one. Even though my demo application (small part shown above) does not use any code from any of these three libraries, some ideas are shared.

—SA
 
Share this answer
 
v4
Comments
Espen Harlinn 16-Mar-11 18:14pm    
Looking forward to reading your article SA - 5ed!
Sergey Alexandrovich Kryukov 16-Mar-11 18:58pm    
Thank you very much.

Procrastinating...

Interesting. I read a big article in Scientific American on procrastination: the nature of it; and how it destroys carrier and life, and so on, and many things are so true, but... a couple issues later in reader's letter one guy says: "You just have no idea what are you fighting against. Procrastination is just an invaluable subconscious tool helping to mature decisions...". I guess it all depends...

--SA
Espen Harlinn 16-Mar-11 19:05pm    
A really looking forward to discovering your take on that - Well, I guess it's that time again, sleep seems an excellent idea just know, as I'm going to need a clear head tomorrow :)
Sergey Alexandrovich Kryukov 16-Mar-11 19:22pm    
Sleep is actually a very active process, a part of it. They say, a person is unable to grow clever without the sleeping-time activity like night dream and such.
So, wish you a good clean-up :-)
--SA
Sergey Alexandrovich Kryukov 29-May-11 23:22pm    
The article is ready,

Dynamic Method Dispatcher,
http://www.codeproject.com/KB/dotnet/DynamicMethodDispatcher.aspx
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900