问题描述
- 怎样把多表查询的内容绑定到listview
-
定义一个变量,将多表查询的结果添加到变量,再把变量里多表查询的结果绑定到listview控件
,下面是代码
TabelDataContext dc = new TabelDataContext();
var query = from s in dc.Employee
//from d in dc.Department
where Convert.ToInt32(s.DID) == 2
select new
{
ID = s.eID,
Name = s.eName,
Sex=s.eSex,
Birthday=s.eBirthday,}; // this.listView1.Items.Add(query); foreach (var result in query) { var u = result; ListViewItem lvi = new ListViewItem(); lvi.Tag = query; lvi.Text = "编号:" + u.ID + "名称:" + u.Name; lvi.Name = u.ID; lvi.SubItems[0] = u.ID;//想把查询结果的字段赋值到集里,显示无法进行转换 lvi.ImageIndex = 0; this.listView1.Items.Add(lvi); 现在想把listview选中的项赋值到textbox里面去,但查询的结果不是一张表里的内容了,那怎么将query里的字段绑定到textbox中以达到实现修改的功能 }
解决方案
不是一张表连接查询就好了。
var query = from s in dc.Employee
from d in dc.Department
where s.DID == d.ID //比如说是这么关联的
where
select
{
ID = s.eID,
Name = s.eName,
Sex=s.eSex,
Birthday=s.eBirthday,
Dep = d.Name
};
解决方案二:
textBox1.Text = query.First().字段;
时间: 2024-10-30 06:08:12