1. 程式人生 > >C# devExpress GridControl 行中行 子行 多級行

C# devExpress GridControl 行中行 子行 多級行

在工作中經常會碰到需要做行中行,多級行的情況,不熟的情況下,我也只能試著實現.

名稱空間

using DevExpress.XtraEditors.Repository;
using System.Data.SqlClient;

實現程式碼

一下實現的也只是一個demo,大家不要拘泥於資料

   DB db = new DB();
            DataSet ds = new System.Data.DataSet();
            SqlCommand comm2 = new SqlCommand(sql, db.getSqlConnection());  //db.getSqlConnection() 返回一個sqlConnection 物件
            SqlCommand comm3 = new SqlCommand(sql1, db.getSqlConnection());
            SqlCommand comm4 = new SqlCommand(sql2, db.getSqlConnection());
            SqlDataAdapter da2 = new SqlDataAdapter(comm2);
            SqlDataAdapter da3 = new SqlDataAdapter(comm3);
            SqlDataAdapter da4 = new SqlDataAdapter(comm3);

            da2.Fill(ds,"emp");
            da3.Fill(ds,"job");
            da4.Fill(ds,"re");

            this.treeListLookUpEdit1TreeList.DataSource = ds;

            DataColumn parentColumn = ds.Tables["emp"].Columns["empNum"];
            DataColumn childColumn = ds.Tables["job"].Columns["empNum"];
            DataColumn secondChild = ds.Tables["re"].Columns["empNum"];

            DataRelation relCustOrder;
            relCustOrder = new DataRelation("對應工作", parentColumn, childColumn);
            DataRelation job;
            job = new DataRelation("部門調整", childColumn, secondChild);
            ds.Relations.Add(relCustOrder);
            ds.Relations.Add(job);
            this.gridControl1.DataSource = ds.Tables["emp"];

實現效果如下: