先附上效果圖,不是想要這個效果的朋友就不用可以繼續尋找了。

DEV—GridControl制作主從表:
(注:此例沒有用到數據庫,只是單純的在內存中操作數據。)
寫這一筆,是為了能更好的理解主從表,的搭建關系。
1.環境:
主表(這里用類代替):
1 /// <summary> 2 /// 主表 3 /// </summary> 4 public class mobile_flash_promotions 5 { 6 7 /// <summary> 8 /// ID 9 /// </summary> 10 public string id { get; set; } 11 12 public string sid { get; set; } 13 14 /// <summary> 15 /// 活動標題 16 /// </summary> 17 public string title { get; set; } 18 19 public string pro_desc { get; set; } 20 21 /// <summary> 22 /// 活動鏈接 23 /// </summary> 24 public string link { get; set; } 25 26 /// <summary> 27 /// 活動開始時間 28 /// </summary> 29 public string start_time { get; set; } 30 31 /// <summary> 32 /// 活動結束時間 33 /// </summary> 34 public string end_time { get; set; } 35 36 /// <summary> 37 /// 活動圖片 38 /// </summary> 39 public string pict { get; set; } 40 41 public string seq { get; set; } 42 43 /// <summary> 44 ///活動 創建時間 45 /// </summary> 46 public string create_time { get; set; } 47 48 /// <summary> 49 /// 活動創建人 50 /// </summary> 51 public string creater { get; set; } 52 53 /// <summary> 54 /// 活動創建分店名稱 55 /// </summary> 56 public string create_shop_name { get; set; } 57 58 /// <summary> 59 /// 活動創建分店sid 60 /// </summary> 61 public string create_shop_sid { get; set; } 62 63 /// <summary> 64 /// 是否是旗艦店 65 /// </summary> 66 public string flag { get; set; } 67 68 /// <summary> 69 /// 活動店鋪集合 70 /// </summary> 71 public List<mobile_promotion_product> ls_p { get; set; } 72 }
從表(仍然是類):
1 /// <summary> 2 /// 從表 3 /// </summary> 4 public class mobile_promotion_product 5 { 6 public string sid { get; set; } 7 8 public string promotion_sid { get; set; } 9 10 public string product_sid { get; set; } 11 12 public string shop_sid { get; set; } 13 14 public string seq { get; set; } 15 }
GridControl中在MainView中顯示主表的GridView,隨后添加一個Level在其中添加從表的GridView,如圖:

來具體說明一下,主表與從表的關系式通過主表類中的List<從表>來確立的。
如果在主從表功能實現后,從表的列名和自己寫好的從表的GridView的列名不相符,那就是在GridControl中添加Level時,Level的name與主表類中代表從表集合的字段名不一樣所導致的。(也就是說,如果不需要定制從表的GridView,完全可以不添加Level和從表的GridView)。
2.模擬數據庫環境:
首先說要實現的效果,【點擊主表Row時,刷新從表信息】
剛才提到過本例子的數據都是在內存中操作,所以首先寫一個加載數據的方法:
1 /// <summary> 2 /// 主表數據源 3 /// </summary> 4 List<mobile_flash_promotions> ls_f = new List<mobile_flash_promotions>(); 5 6 /// <summary> 7 /// 從表數據源 8 /// </summary> 9 List<mobile_promotion_product> ls_p = new List<mobile_promotion_product>(); 10 11 /// <summary> 12 /// 向數據源加載數據 13 /// </summary> 14 public void LoadBaseData() 15 { 16 17 mobile_promotion_product mpp1 = new mobile_promotion_product() { sid = "1", product_sid = "MS10001", promotion_sid = "1", shop_sid = "1", seq = "1" }; 18 mobile_promotion_product mpp2 = new mobile_promotion_product() { sid = "2", product_sid = "MS10002", promotion_sid = "1", shop_sid = "1", seq = "0" }; 19 mobile_promotion_product mpp3 = new mobile_promotion_product() { sid = "3", product_sid = "MS10003", promotion_sid = "2", shop_sid = "1", seq = "1" }; 20 mobile_promotion_product mpp4 = new mobile_promotion_product() { sid = "4", product_sid = "MS10004", promotion_sid = "2", shop_sid = "2", seq = "0" }; 21 22 ls_p.Add(mpp1); 23 ls_p.Add(mpp2); 24 ls_p.Add(mpp3); 25 ls_p.Add(mpp4); 26 27 mobile_flash_promotions mfp1 = new mobile_flash_promotions() { id = "1", sid = "1", title = "第一條活動", pro_desc = "測試用例", link = "www.baidu.com", start_time = "2014-03-24", end_time = "2014-04-03", pict = "/picture/111000.jpg", seq = "1", create_time = "2014-03-24", creater = "shiying", create_shop_name = "海淀區", create_shop_sid = "10", flag = "1" }; 28 mobile_flash_promotions mfp2 = new mobile_flash_promotions() { id = "2", sid = "2", title = "第二條活動", pro_desc = "測試", link = "www.shopin.net", start_time = "2013-02-13", end_time = "2014-04-03", pict = "/picture/qdq.jpg", seq = "2", create_time = "2013-05-30", creater = "shiying", create_shop_name = "朝陽區", create_shop_sid = "11", flag = "0" }; 29 30 ls_f.Add(mfp1); 31 ls_f.Add(mfp2); 32 }
然后我們添加一個Timer進來,設置每5秒添加一條數據,Enable=True。
1 /// <summary> 2 /// 標記從ID=5開始添加 3 /// </summary> 4 int i = 4; 5 6 /// <summary> 7 /// 每5秒添加一次數據 8 /// </summary> 9 /// <param name="sender"></param> 10 /// <param name="e"></param> 11 private void timer1_Tick(object sender, EventArgs e) 12 { 13 i = i + 1; 14 15 mobile_promotion_product mpp5 = new mobile_promotion_product() { sid = (i).ToString(), product_sid = "MS1000" + (i).ToString(), promotion_sid = "2", shop_sid = "3", seq = "0" }; 16 17 ls_p.Add(mpp5); 18 }
數據添加完了,下一步是查詢:
1 /// <summary> 2 /// 查詢 3 /// </summary> 4 public void Query() 5 { 6 7 List<mobile_promotion_product> ls_p_f1 = ls_p.Where(p => p.promotion_sid == ls_f[0].sid).ToList(); 8 List<mobile_promotion_product> ls_p_f2 = ls_p.Where(p => p.promotion_sid == ls_f[1].sid).ToList(); 9 10 ls_f[0].ls_p = ls_p_f1; 11 ls_f[1].ls_p = ls_p_f2; 12 13 gc_Photosys.DataSource = ls_f; 14 }
基本代碼都oK了,剩下的是何時調用查詢,本例的查詢時發生在點擊主表的Row的第一個Cell時發生,即向主表的GridView添加RowCellClick事件:
1 /// <summary> 2 /// 主表選中行發生改變時折疊上一個展開的從表+點擊主表Row查詢從表 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void gv_f_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e) 7 { 8 if (gv_f_rowhandle != e.RowHandle) 9 { 10 //折疊從表 11 gv_f.SetMasterRowExpanded(gv_f_rowhandle, false); 12 13 gv_f_rowhandle = e.RowHandle; 14 } 15 16 if (e.Column.ColumnHandle == 0) 17 { 18 Query(); 19 } 20 }
將主表類的集合綁定到主表的GridView中(MainView的GridView) 后,主從表就實現了!
總結:主從表的實現相對於DEV來說很簡單,只需要在主表的每個對象中添加一個從表對象的集合即可。
