如何解决如何将多个列表的内容添加到 ListView 项和子项中?
我有几个列表,其中包含赛事名称和赛事类型,无论是团队赛事还是个人赛事。每个事件的名称和事件类型以分号分隔。
Properties.Settings.Default.eventName = "Chess;Football;Paintball;Swimming";
Properties.Settings.Default.eventName = "indiv;team;team;indiv";
List<string> eventName = Properties.Settings.Default.eventName.Split(';').ToList();
List<string> eventType = Properties.Settings.Default.eventType.Split(';').ToList();
我希望以列表的形式显示这些内容,以便以后可以轻松添加更多事件。在下面的配置中,如何将这些添加到 ListView 中,以事件名称作为项目,事件类型作为子项目?
_________________________
| Name | Type | <--- column names
|____________|____________|
| Chess | Individual |
|____________|____________|
| Football | Team |
|____________|____________|
| Paintball | Team |
|____________|____________|
| Swimming | Individual |
|____________|____________|
到目前为止我所拥有的。这只是添加项目,不知道如何添加子项目,因为它说“ListView 不包含子项目的定义”。
foreach (string eventNameItem in eventName)
{
lvEvents.Items.Add(eventNameItem);
}
解决方法
使用以下代码。我认为它有效。
Properties.Settings.Default.eventName = "Chess;Football;Paintball;Swimming";
Properties.Settings.Default.eventType = "indiv;team;team;indiv";
List<string> eventName = Properties.Settings.Default.eventName.Split(';').ToList();
List<string> eventType = Properties.Settings.Default.eventType.Split(';').ToList();
for(int i=0;i< eventName.Count; i++)
{
string[] row = { eventName[i],eventType[i] };
ListViewItem item = new ListViewItem(row);
listView1.Items.Add(item);
}
或使用
//Width of -2 indicates auto-size.
listView1.Columns.Add("eventName",-2,HorizontalAlignment.Left);
listView1.Columns.Add("eventType",HorizontalAlignment.Left);
for (int i = 0; i < eventName.Count; i++)
{
string[] row = { eventName[i],eventType[i] };
ListViewItem item = new ListViewItem(row);
listView1.Items.Add(item);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。