1. 程式人生 > >10.17小結:table.copy() 和 distinct 查詢

10.17小結:table.copy() 和 distinct 查詢

1. 當datatable 已存在於一個dataset中是,可以使用 ds.tables.add(dt.copy()) 來想dataset 中新增datatable;

2. 在查詢表格時,如果想查詢兩個欄位,只當兩個欄位都相同時才過濾重複保留唯一,否則不過濾,這是可以這樣使用distinct:

select  distinct  a, b  from  table1

3. 今天在往devexpress 中的treelist 控制元件中新增節點時,發現雖然新增上了但是介面不顯示節點值,解決辦法是在設計器裡先給treelist新增一個列(當然,我的需求是隻有一列的情況下),然後隱藏它即可。為什麼要新增一列呢?因為在後臺程式碼裡我們是這樣寫的:

TreeListNode FirstLevelNode = treeList1.AppendNode(null, root);
FirstLevelNode.SetValue(treeList1.Columns[0], drFirstLevelNode["StateName"].ToString().Trim());

這裡的  treeList1.Columns[0]  就是我們新增的那一行(我自己理解的,不知道對不對,反正實踐顯示不新增就沒法顯示)。

最後附上完整的往dev treelist 中根據資料庫中資料動態新增節點程式碼:

WellDAL dalWell = new
WellDAL(); TreeListNode root = treeList1.AppendNode(null, null); root.SetValue(treeList1.Columns[0], "本地資料庫"); //第一層節點 DataTable dtFirstLevelNode = new DataTable(); dtFirstLevelNode = dalWell.GetData("StateName"); foreach (DataRow drFirstLevelNode in
dtFirstLevelNode.Rows) { TreeListNode FirstLevelNode = treeList1.AppendNode(null, root); FirstLevelNode.SetValue(treeList1.Columns[0], drFirstLevelNode["StateName"].ToString().Trim()); //第二層節點 DataTable dtSecondLevelNode = new DataTable(); dtSecondLevelNode = dalWell.GetData("StateName", "FieldName"); foreach (DataRow drSecondLevelNode in dtSecondLevelNode.Rows) { if (drSecondLevelNode["StateName"].ToString().Trim() == drFirstLevelNode["StateName"].ToString().Trim()) { TreeListNode SecondLevelNode = treeList1.AppendNode(null, FirstLevelNode); SecondLevelNode.SetValue(treeList1.Columns[0], drSecondLevelNode["FieldName"].ToString().Trim()); //第三層節點 DataTable dtThirdLevelNode = new DataTable(); dtThirdLevelNode = dalWell.GetData("StateName", "FieldName", "BlockName"); foreach (DataRow drThirdLevelNode in dtThirdLevelNode.Rows) { if (drThirdLevelNode["StateName"].ToString().Trim() == drFirstLevelNode["StateName"].ToString().Trim() && drThirdLevelNode["FieldName"].ToString().Trim() == drSecondLevelNode["FieldName"].ToString().Trim()) { TreeListNode ThirdLevelNode = treeList1.AppendNode(null, SecondLevelNode); ThirdLevelNode.SetValue(treeList1.Columns[0], drThirdLevelNode["BlockName"].ToString().Trim()); //第四層節點 DataTable dtForthLevelNode = new DataTable(); dtForthLevelNode = dalWell.GetData("StateName", "FieldName", "BlockName", "WellName"); foreach (DataRow drForthLevelNode in dtForthLevelNode.Rows) { if (drForthLevelNode["StateName"].ToString().Trim() == drFirstLevelNode["StateName"].ToString().Trim() && drForthLevelNode["FieldName"].ToString().Trim() == drSecondLevelNode["FieldName"].ToString().Trim() && drForthLevelNode["BlockName"].ToString().Trim() == drThirdLevelNode["BlockName"].ToString().Trim()) { TreeListNode ForthLevelNode = treeList1.AppendNode(null, ThirdLevelNode); ForthLevelNode.SetValue(treeList1.Columns[0], drForthLevelNode["WellName"].ToString().Trim()); //第五層節點 DataTable dtFifthLevelNode = new DataTable(); dtFifthLevelNode = dalWell.GetData("StateName", "FieldName", "BlockName", "WellName", "HoleNumber"); foreach (DataRow drFifthLevelNode in dtFifthLevelNode.Rows) { if (drFifthLevelNode["StateName"].ToString().Trim() == drFirstLevelNode["StateName"].ToString().Trim() && drFifthLevelNode["FieldName"].ToString().Trim() == drSecondLevelNode["FieldName"].ToString().Trim() && drFifthLevelNode["BlockName"].ToString().Trim() == drThirdLevelNode["BlockName"].ToString().Trim() && drFifthLevelNode["WellName"].ToString().Trim() == drForthLevelNode["WellName"].ToString().Trim()) { TreeListNode FifthLevelNode = treeList1.AppendNode(null, ForthLevelNode); FifthLevelNode.SetValue(treeList1.Columns[0], drFifthLevelNode["HoleNumber"].ToString().Trim()); } } } } } } } } } treeList1.ExpandAll();

 效果圖: