简体   繁体   中英

Listview Add Items not appearing C#

I'm working on a small program as part of my A Level Computing course that is designed to track orders. It is written in C# using the Windows Forms.

I am having an issue where I enter all the information for a new order and then press OK and it should update the ListView with the information. I have my ListView in Detail view with 4 columns but nothing ever gets added to the ListView. The section of code that should add the items to the ListView is being executed and is not throwing any errors or causing the program to crash but nothing is being added. Its weird because I am using the exact same method that I used in my little prototype mock up but for some reason now it is not working.

All the things I've found on here or on the internet seem to suggest its an issue with the View mode of the ListView and I've tried modifying this property to no avail.

Any ideas why this section of code is refusing to add anything to the ListView?

            //Create an array to store the data to be added to the listbox
            string[] orderDetails = { Convert.ToString(id + 1), rNameBox.Text, dateBox.Value.ToString(), orderBox.Text };

            //DEBUGGING
            Console.WriteLine(orderDetails[0]);
            Console.WriteLine(orderDetails[1]);
            Console.WriteLine(orderDetails[2]);
            Console.WriteLine(orderDetails[3]);
            //END DEBUGGING

            //Add the order info to the ListView item on the main form
            var listViewItem = new ListViewItem(orderDetails);
            ths.listView1.Items.Add(listViewItem);

If you need any more information just say. Apologies if this is in the wrong format or something this is my first time here.

Your problem is that your ListViewItem contains a string array and it has no useful way of displaying it.

What you should be doing (there are a number of ways of doing this, but here's one) is creating a class, OrderDetail, with an Id, a Name, a Date, and so on. Give it a ToString() method (public override string ToString()) which returns what you want to display, eg:

public override string ToString()
{
    return this.Name;
}

Create an instance of OrderDetail and set its properties. Create ListViewItem giving it the OrderDetail instance and add to the ListView. Repeat for as many OrderDetail instances you want.

Cheers -

Added: code which works:

    int id = 12;
    string rNameBoxText = "rName";
    DateTime dateBoxValue = DateTime.Now;
    string orderBoxText = "order";
    string[] orderDetails = { Convert.ToString(id + 1), rNameBoxText, dateBoxValue.ToString(), orderBoxText };

    //DEBUGGING
    Console.WriteLine(orderDetails[0]);
    Console.WriteLine(orderDetails[1]);
    Console.WriteLine(orderDetails[2]);
    Console.WriteLine(orderDetails[3]);
    //END DEBUGGING

    this.listView1.Columns.Clear();
    this.listView1.Columns.Add("Id");
    this.listView1.Columns.Add("rName");
    this.listView1.Columns.Add("Date");
    this.listView1.Columns.Add("Order");
    this.listView1.View = View.Details;
    //Add the order info to the ListView item on the main form
    var listViewItem = new ListViewItem(orderDetails);
    this.listView1.Items.Add(listViewItem);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM