您好我正在尝试使用 EF4 从数据库中填充 Listview。我试图用下面的代码填充,但列表视图中没有填充,尽管项目正在获取。请帮助我如何进行。
private void FillListView()
{
using (DiagnosoftDataContext context = new DiagnosoftDataContext())
{
var user = from c in context.Users
select new
{
Code = c.name,
Name = c.age,
Sex = c.sex,
DOB = c.dob,
};
//Bind the ListView
// Create items and subitems for each item.
listView1.View = View.Details;
ListViewItem lv = new ListViewItem("lv", 0);
var item = user.ToList();
foreach (var u in item)
{
lv.SubItems.Add(item[0].Code);
lv.SubItems.Add(item[0].Name);
lv.SubItems.Add(item[0].Sex);
lv.SubItems.Add(item[0].DOB);
}
// Create columns for the items and subitems.
listView1.Columns.Add("Name", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Age", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Sex", -2, HorizontalAlignment.Left);
listView1.Columns.Add("DOB", -2, HorizontalAlignment.Left);
}
}
提前致谢。