表单调用InitializeComponent
后,我创建了一个List<DeletionTarget>
:
var deletionList = new List<DeletionTarget>();
deletionList.Add(new DeletionTarget("path1", new DateTime(2016, 1, 10), false));
deletionList.Add(new DeletionTarget("path2", new DateTime(2016, 2, 10), true));
deletionList.Add(new DeletionTarget("path3", new DateTime(2016, 3, 10), false));
deletionList.Add(new DeletionTarget("path4", new DateTime(2016, 4, 10), true));
deletionList.Add(new DeletionTarget("path5", new DateTime(2016, 5, 10), false));
DeletionTarget
是一个具有以下属性的简单对象:
public string Path;
public DateTime Period;
public bool Recurse;
public DeletionTarget(string path, DateTime period, bool recurse)
{
Path = path;
Period = period;
Recurse = recurse;
}
接下来,我调用InitializeView()
方法:
public void InitializeListView()
{
var header1 = listView1.Columns.Add("Path", -2, HorizontalAlignment.Left);
var header2 = listView1.Columns.Add("Period", -2, HorizontalAlignment.Left);
var header3 = listView1.Columns.Add("Recurse", -2, HorizontalAlignment.Left);
}
这应该向ListView
添加一些列(DeletionTarget
对象中的每个属性一列)。现在是时候将数据添加到ListView
:
foreach (var item in deletionList)
{
var lvi = new ListViewItem(new[] { item.Path, item.Period.ToString(), item.Recurse.ToString() });
listView1.Items.Add(lvi);
}
当我运行程序时,这就是我的表单:
正如你所看到的,这是没用的。我需要每个项目显示在一个单独的行上,每个属性都有一列。有人可以帮我理解我在这里做错了吗?
谢谢