我们已经给出了一个使用IList来填充列表框的示例。我想使用相同的过程来填充列表中的对象,但填充ListView。
来自对象类的代码如下所示:
public void ListClients(IList list)
{
list.Clear();
for (int i = 0; i < MAX_CLIENTS; ++i)
{
if (myClients[i] == null)
continue;
list.Add(myClients[i].FullName);
}
}
表单中的代码,如下所示:
private void ListClientButton_Click(object sender, EventArgs e)
{
CDB.ListClients(ListClientsBox.Items);
}
我尝试了很多方法,但我想我对IList概念不够了解。是否可以在IList的详细信息视图中填写列表视图?
答案 0 :(得分:2)
您的ListView
控件是否设置了任何列?如果没有,内容将不可见。尝试:
private void ListClientButton_Click(object sender, EventArgs e)
{
ListClientsBox.Columns.Add("Full name");
CDB.ListClients(ListClientsBox.Items);
}
修改强>
我已经运行了一些测试,你在评论中发布的代码似乎没问题。您可能只需调整一些ListView的属性,具体取决于您的目标:
// perpare ListView beforehand
this.listView.Columns.Add("First name");
this.listView.Columns.Add("Email");
this.listView.Columns.Add("Country");
this.listView.View = View.Tile;
// if tile height is too small, some data might not be visible
this.listView.TileSize = new Size(180, 50);
// sample data
var people = new[]
{
new { FirstName = "John", Email = "john@domain.com", Country = "USA" },
new { FirstName = "Betty", Email = "betty72@mail.org", Country = "Canada" },
new { FirstName = "Steven", Email = "stv@domain.net", Country = "Brazil" },
};
foreach (var person in people)
{
ListViewItem item = new ListViewItem(person.FirstName);
item.SubItems.Add(person.Email);
item.SubItems.Add(person.Country);
this.listView.Items.Add(item);
}
这就是ListView的样子,View.Tile
和View.Details
: