GridControl主从表设置

2023-12-26 11:58:10
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];//设定主表

使GridControl在父容器停靠,并在,gridView1下面添加另一个gridView2。其中第一为主表显示,第二个为子表显示和,注意等级帐户信息,不然不显示

DataTable dt1 = new DataTable();
            {
                dt1.Columns.Add("编号");
                dt1.Columns.Add("姓名");
                DataRow dr = dt1.NewRow();
                dr[0] = "001";
                dr[1] = "张三";
                dt1.Rows.Add(dr);
                dr = dt1.NewRow();
                dr[0] = "002";
                dr[1] = "李四";
                dt1.Rows.Add(dr);
            }
                DataTable dt2 = new DataTable();
            {
                dt2.Columns.Add("编号");
                dt2.Columns.Add("帐户金额");
                DataRow dr = dt2.NewRow();
                dr[0] = "001";
                dr[1] = "100000";
                dt2.Rows.Add(dr);
                dr = dt2.NewRow();
                dr[0] = "002";
                dr[1] = "2000000";
                dt2.Rows.Add(dr);
            }

            DataSet ds = new DataSet();
            ds.Tables.Add(dt1);
            ds.Tables.Add(dt2);
            DataRelation relation = new DataRelation("帐户信息", ds.Tables[0].Columns["编号"], ds.Tables[1].Columns["编号"]);
            ds.Relations.Add(relation);
            gridControl1.DataSource = ds.Tables[0];

文章来源:https://blog.csdn.net/wushijun5200/article/details/135210313
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。