【发布时间】:2013-11-18 23:54:50
【问题描述】:
我正在开发一个供个人使用的项目(简单的电话簿)。 This is how it looks like:
我有一个列表视图,里面装满了联系人,一切正常,直到我添加了这行代码,然后一切都变得一团糟。
listView1.Sorting = SortOrder.Ascending;
所以,问题很明显。假设列表中有 6 个联系人,联系人 1 住在 City 1,在 Address 1 上,有一个 电话.没有 1 [等],联系人 2 住在 City 2,在 Address 2,有一个 电话。没有 2 等[...序列继续...]
当我尝试进行一些更改时,例如,联系人 5 突然获取了其他联系人的信息,在本例中为 联系人 7。不管它得到谁的信息,问题是一切都变得一团糟。
另外,如果我想从列表视图中删除所有联系人 - 这是不可能的 - 总会有一个剩余的。例如,如果有 6 个,则删除 5 个,剩下 1 个。此外,如果有 100 个,它将删除 99 个,并且始终保留一个。
我发现不再使用所选项目的索引没有意义,我现在必须使用所选项目的值(而不是其索引)。但是,问题是我不知道该怎么做。
也许只涉及 listView1_SelectedIndexChanged。请注意,我只是猜测,我并不完全确定。
如果是这样,这里是代码:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0) return;
textBox1.Text = people[listView1.SelectedItems[0].Index].Name;
textBox2.Text = people[listView1.SelectedItems[0].Index].Hometown;
textBox3.Text = people[listView1.SelectedItems[0].Index].Address;
textBox4.Text = people[listView1.SelectedItems[0].Index].Phone;
textBox5.Text = people[listView1.SelectedItems[0].Index].Email;
textBox6.Text = people[listView1.SelectedItems[0].Index].AdditionalInfo;
dateTimePicker1.Value = people[listView1.SelectedItems[0].Index].Birthday;
textBox1.ReadOnly = true;
textBox2.ReadOnly = true;
textBox3.ReadOnly = true;
textBox4.ReadOnly = true;
textBox5.ReadOnly = true;
textBox6.ReadOnly = true;
dateTimePicker1.Enabled = false;
toolStripButton5.Enabled = true;
}
我觉得代码太大了在这里上传,所以I uploaded the code here:
有人有解决办法吗?
【问题讨论】:
标签: c# listview selecteditem listviewitem