注:GridControl中包含了GridView 添加GridControl1
连接数据库:
string souce ="server=(local);Integrated Security=SSPI;database=dx";
SqlConnection conn =new SqlConnection(souce);
conn.Open();
string str ="select * from base_color";
string str2 ="select * from base_detail";
string str3 ="select * from base_three";
SqlDataAdapter da =new SqlDataAdapter(str, con);
SqlDataAdapter da2 =new SqlDataAdapter(str2, con);
SqlDataAdapter da3 =new SqlDataAdapter(str3, con);
DataSet ds =new DataSet();
a.Fill(ds, "color");
da2.Fill(ds, "detail");
da3.Fill(ds, "three");
DataRelation dr = new DataRelation("2层", ds.Tables["color"].Columns["id"],ds.Tables["detail"].Columns["color_no"]);
//设定关系,ds.color(表). Id(字段)= ds.detail. color_no
DataRelation dr2 = new DataRelation("3层", ds.Tables["color"].Columns["id"],ds.Tables["three"].Columns["color_no"]);
DataRelation dr3 = new DataRelation("4层", ds.Tables["detail"].Columns["id"],ds.Tables["three"].Columns["color_no"]);
ds.Relations.Add(dr);
ds.Relations.Add(dr2);
ds.Relations.Add(dr3);
gridControl1.DataSource = ds.Tables[0];//设定主表