1 DataSet ds = new DataSet();
2 string str = "SELECT ID,姓名,性别,年龄 FROM AA ";
3 DataTable main = new DataTable();
4 main.TableName = "aa";
5 //填充main表代码略…
6 ds.Tables.Add(main);
7
8 string sel = "SELECT ID,学科,成绩 FROM BB";
9 DataTable sub = new DataTable();
10 sub.TableName = "bb";
11 //填充sub表代码略…
12 ds.Tables.Add(sub);
13
14 ds.Relations.Add("mainandsub", ds.Tables["aa"].Columns["ID"], ds.Tables["bb"].Columns["ID"]);
15
16 bindingSource1.DataSource = ds;
17 bindingSource1.DataMember = "aa";
18 bindingSource2.DataSource = bindingSource1;
19bindingSource2.DataMember = "mainandsub";
20
以上代码构建了父子表关系,我想查找学生“ID”等于23的语文成绩,如果没有对应的成绩将不显示该学生的详细信息,请问一下该怎么实现,(在BindingSource内查找,.net2.0平台)
int itemFound = bindingSource1.Find("ID",23);
bindingSource1.Position = itemFound;
可以通过联表查询啊!SELECT 成绩,姓名,性别,年龄 FROM AA as a ,BB as b where a.ID=b.ID and ID=23,然后再绑定数据在DataTable中去使用就行了!
从bindingSource1数据里面循环查找ID='23',成绩字段值,没有的成绩的话,从bindingSource1进行删除
用视图查询