简体   繁体   English

Listview添加项不出现C#

[英]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. 我正在做一个小程序,这是我的A Level计算课程的一部分,该课程旨在跟踪订单。 It is written in C# using the Windows Forms. 它是使用Windows窗体以C#编写的。

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. 我遇到一个问题,我要输入新订单的所有信息,然后按OK,它应该使用该信息更新ListView。 I have my ListView in Detail view with 4 columns but nothing ever gets added to the ListView. 我有4列的详细信息视图中的ListView,但没有任何东西被添加到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. 应该将项目添加到ListView的代码部分正在执行,并且不会引发任何错误或不会导致程序崩溃,但是没有添加任何内容。 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. 我在这里或在互联网上找到的所有内容似乎都表明它与ListView的View模式有关,并且我尝试将该属性修改为无济于事。

Any ideas why this section of code is refusing to add anything to the ListView? 有什么想法为什么这段代码拒绝向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. 您的问题是ListViewItem包含一个字符串数组,并且没有显示它的有用方法。

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. 您应该做的事情(有很多方法可以做到这一点,但这是一种)是创建一个类,OrderDetail,以及一个ID,一个名称,一个日期等等。 Give it a ToString() method (public override string ToString()) which returns what you want to display, eg: 给它一个ToString()方法(公共重写字符串ToString()),该方法返回要显示的内容,例如:

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

Create an instance of OrderDetail and set its properties. 创建OrderDetail的实例并设置其属性。 Create ListViewItem giving it the OrderDetail instance and add to the ListView. 创建给它提供OrderDetail实例的ListViewItem并添加到ListView。 Repeat for as many OrderDetail instances you want. 重复所需的OrderDetail实例。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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