列表视图中的空项目 [英] Empty items in listview
问题描述
我的问题是,当我从Form2的文本框中插入值时,Listview(Form1)中的项目为空.我真的不知道为什么它是空的.有人帮我提供此代码吗?!
My problem is when i insert values from textboxes in Form2 the items in Listview (Form1) is empty. I dont really know why its empty. Somebody help me with this code please?!
InitializeComponent();
listView1.View = View.Details;
listView1.LabelEdit = true;
listView1.AllowColumnReorder = true;
listView1.FullRowSelect = true;
listView1.GridLines = true;
listView1.Sorting = SortOrder.Ascending;
listView1.FullRowSelect = true;
listView1.Columns.Add("ID", 300, HorizontalAlignment.Left);
listView1.Columns.Add("Name", 70, HorizontalAlignment.Left);
listView1.Columns.Add("Zipcode", 70, HorizontalAlignment.Left);
listView1.Columns.Add("City", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Country", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Phone", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Email", 100, HorizontalAlignment.Left);
Form2
Form2
private void btnOk_Click(object sender, EventArgs e)
{
contact.FirstName = tbFirstName.Text;
firstName = contact.FirstName;
contact.LastName = tbLastName.Text;
lastName = contact.LastName;
Form2
Form2
private void btnOk_Click(object sender, EventArgs e)
{
MainForm main = new MainForm();
contact.FirstName = tbFirstName.Text;
firstName = contact.FirstName;
contact.LastName = tbLastName.Text;
lastName = contact.LastName;
Form2
Form2
public override string ToString()
{
return string.Format("[{0}]", contact.ToString());
}
接触类
Contact class
class Contact
{
private string firstName;
private string lastName
在Contact类中,还具有上述变量的属性,然后是像这样的ToString
In the Contact class there is also properties of the above variables and then a ToString like this
public override string ToString()
{
return string.Format("[{0}, {1}, {2}, {3}]", firstName, lastName);
}
Form1
Form1
private void MainForm_Load(object sender, EventArgs e)
{
ColumnHeader columnheader;
ListViewItem listviewitem;
// Ensure that the view is set to show details.
listView1.View = View.Details;
if (customerframe.ShowDialog() == DialogResult.OK) //if button OK is clicked then value will be inserted
{
listviewitem = new ListViewItem(contact.FirstName);
listviewitem.SubItems.Add(contact.LastName);
this.listView1.Items.Add(listviewitem);
推荐答案
第一个错误我可以看到:
First bug I can see it this:
return string.Format("[{0}, {1}, {2}, {3}]", firstName, lastName);
为此格式,您需要四个参数,但仅提供两个.
另外,除最左边的一个单元格外的所有单元格都通过SubItems
填充.我发现只有一个使用此属性的地方.您将FirstName
添加到第一列,并在下一列中填写LastName
.但是您有7列,它们分别被命名为"ID",名称",邮政编码"等,而不是姓氏/名字.您在哪里填写其余的列?难怪你有空值...
For this format, you need four parameters, but you provide only two.
Also, all cells except leftmost one are filled via SubItems
. I found only one place where this property is used. You add FirstName
to a first column and fill in LastName
in next one. But you have 7 columns, and they are named "ID", "Name", "Zipcode", etc., not Last/First name. Where do you fill in the rest of the columns? no wonder you have empty values…
我看到了一些愚蠢的错误并予以解决
i saw some stupid misstakes and fixed it
这篇关于列表视图中的空项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!