[英]Listview Add Items not appearing C#
我正在做一个小程序,这是我的A Level计算课程的一部分,该课程旨在跟踪订单。 它是使用Windows窗体以C#编写的。
我遇到一个问题,我要输入新订单的所有信息,然后按OK,它应该使用该信息更新ListView。 我有4列的详细信息视图中的ListView,但没有任何东西被添加到ListView中。 应该将项目添加到ListView的代码部分正在执行,并且不会引发任何错误或不会导致程序崩溃,但是没有添加任何内容。 这很奇怪,因为我使用的是与我的小原型模型中使用的完全相同的方法,但是由于某种原因,现在它无法正常工作。
我在这里或在互联网上找到的所有内容似乎都表明它与ListView的View模式有关,并且我尝试将该属性修改为无济于事。
有什么想法为什么这段代码拒绝向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);
如果您需要更多信息,请说。 抱歉,如果格式错误或这是我第一次来。
您的问题是ListViewItem包含一个字符串数组,并且没有显示它的有用方法。
您应该做的事情(有很多方法可以做到这一点,但这是一种)是创建一个类,OrderDetail,以及一个ID,一个名称,一个日期等等。 给它一个ToString()方法(公共重写字符串ToString()),该方法返回要显示的内容,例如:
public override string ToString()
{
return this.Name;
}
创建OrderDetail的实例并设置其属性。 创建给它提供OrderDetail实例的ListViewItem并添加到ListView。 重复所需的OrderDetail实例。
干杯-
补充:有效的代码:
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.