daigua1987 05月09日
DataGridView的主从表问题
sql = "select * from v_" + from.Name.Substring(1, from.Name.Length - 1) + " order by " + from.Name.Substring(1, from.Name.Length - 1) + "ID";
sqlItem= "select * from v_" + from.Name.Substring(1, from.Name.Length - 1) + "Item" + " order by " + from.Name.Substring(1, from.Name.Length - 1) + "ItemID";
DataGridView dv = from.Controls.Find("dataGridView", true)[0] as DataGridView;
DataGridView dvi = from.Controls.Find("dataGridViewItem", true)[0] as DataGridView;
dv.AutoGenerateColumns = false;
dvi.AutoGenerateColumns = false;
DataSet ds = new DataSet();
SqlDataAdapter masterda = new SqlDataAdapter(sql, mydate.Conn);
masterda.Fill(ds, "v_" + from.Name.Substring(1, from.Name.Length - 1));
SqlDataAdapter detailsda = new SqlDataAdapter(sqlItem, mydate.Conn);
detailsda.Fill(ds, "v_" + from.Name.Substring(1, from.Name.Length - 1)+"Item");
DataRelation relation = new DataRelation("ZC",
ds.Tables["v_" + from.Name.Substring(1, from.Name.Length - 1)].Columns[from.Name.Substring(1, from.Name.Length - 1)+"ID"],
ds.Tables["v_" + from.Name.Substring(1, from.Name.Length - 1) + "Item"].Columns[from.Name.Substring(1, from.Name.Length - 1)+"ID"]);
ds.Relations.Add(relation);
dv.DataSource = ds;
dv.DataMember = "v_" + from.Name.Substring(1, from.Name.Length - 1);//设置绑定的主表名称
dvi.DataSource = dv;//设置从表数据源
dvi.DataMember = "ZC";//设置绑定的主从表关系名称

运行提示无法创建字段 ZC 的子列表
网上找的一个例子复制过来的
...全文
83 点赞 收藏 11
写回复
11 条回复

还没有回复,快来抢沙发~

发动态
发帖子
C#
创建于2007-09-28

8.4w+

社区成员

64.0w+

社区内容

.NET技术 C#
社区公告
暂无公告