使用winform我试图使用下面的代码从数据库中将按类别分组的食物项目显示到radlistview中;但问题在于它显示的是主要群组,但未显示其下的项目。
ZalaqEntities1 zlq=new ZalaqEntities1();
List<cat> cats = zlq.cats.ToList();
radListView1.Items.Clear();
radListView1.Groups.Clear();
radListView1.EnableGrouping = true;
radListView1.ShowGroups = true;
for (int i = 0; i < cats.Count(); i++)
{
radListView1.Groups.Add(new ListViewDataItemGroup());
radListView1.Groups[i].Value = cats[i].catname;
radListView1.Groups[i].Key = cats[i].catid;
}
List<fooditem> fooditems = zlq.fooditems1.ToList();
for (int j = 0; j < fooditems.Count(); j++)
{
ListViewDataItem myitem = new ListViewDataItem();
myitem.Value = fooditems[j].itemname;
myitem.Key = fooditems[j].itemid;
// myitem.Group = radListView1.Groups[1];
radListView1.Items.Add(myitem);
myitem.Group = radListView1.Groups.Where(i => i.Key == fooditems[j].catid).FirstOrDefault();
//radListView1.Items[j].Group = radListView1.Groups[1];
}
radListView1.Refresh();
我正在尝试使用where条件在其类别下添加Item,以通过其Key找到正确的组。
如果我在Loop外面执行此操作,则可以在for循环中添加几个Item。但是来自数据库。
答案 0 :(得分:0)
问题只是在Where子句中强制转换i.Key.ToString()。
最终守则:
ZalaqEntities1 zlq=new ZalaqEntities1();
List<cat> cats = zlq.cats.ToList();
radListView1.Items.Clear();
radListView1.Groups.Clear();
radListView1.EnableGrouping = true;
radListView1.ShowGroups = true;
for (int i = 0; i < cats.Count(); i++)
{
ListViewDataItemGroup grp = new ListViewDataItemGroup();
grp.Value = cats[i].catname;
grp.Key = cats[i].catid.ToString();
radListView1.Groups.Add(grp);
radListView1.Groups[i].Key = cats[i].catid;
}
radListView1.Refresh();
List<fooditem> mfooditems = zlq.fooditems1.ToList();
for (int j = 0; j < mfooditems.Count(); j++)
{
ListViewDataItem myitem = new ListViewDataItem();
myitem.Value = mfooditems[j].itemname;
myitem.Key = mfooditems[j].itemid;
radListView1.Items.Add(myitem);
radListView1.Refresh();
radListView1.Items[j].Group = radListView1.Groups.Where(i => i.Key.ToString() == mfooditems[j].catid.ToString()).FirstOrDefault();
}
radListView1.Refresh();