原創|使用教程|編輯:龔雪|2021-02-26 10:29:47.507|閱讀 298 次
概述:本文要介紹如何快速實現樹形列表和分頁查詢整合的Winform程序界面。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
DevExpress擁有.NET開發需要的所有平臺控件,包含600多個UI控件、報表平臺、DevExpress Dashboard eXpressApp 框架、適用于 Visual Studio的CodeRush等一系列輔助工具。
在做Winform界面的時候,一般都是統一化處理,界面頂部放置一些字段條件供查詢,下面就是分頁查詢列表,展示相關的數據。但有時候碰到一些表字段內容分類比較多,有一些特別重要,如果放在一個樹形列表來進行快速分類查詢,用戶體驗應該更好。本文要介紹如何快速實現樹形列表和分頁查詢整合的Winform程序界面。
標準的查詢條件+列表數據展示的WInform界面如下所示。
這個界面主要就是通過代碼生成工具()進行初期的Winform界面生成即可。
以上的界面有時候感覺不夠友好,正如文章開頭說到,我需要在左邊放置一些重要的數據分類進行查詢,這樣能夠提高用戶體驗效果,最終希望的界面效果如下所示。
為了實現這種效果,我們需要進行幾部操作。
在標準列表界面上增加窗口分割控件(如DevExpress的是SplitContainerControl控件)
傳統的Winform界面可以使用SplitContainer控件
在現有已生成界面的基礎上,把查詢部分和列表部分的控件拖動小一點,然后把上述分隔控件拖動到界面后,在右邊面板放入已有的查詢和分頁控件部分的內容,中間狀態的列表界面效果如下所示。
然后在左邊放入一個GroupControl控件,并加入樹形控件TreeView,這樣我們調整后的設計界面效果如下所示。
首先我們需要在代碼里面綁定樹的初始化代碼,生成需要快速查詢的內容,示意代碼如下所示。主要邏輯思路就是,從數據字典中檢索相關的分類,然后綁定一些查詢條件,方便后面的處理。
private void InitTree() { base.LoginUserInfo = Cache.Instance["LoginUserInfo"] as LoginUserInfo; this.treeView1.BeginUpdate(); this.treeView1.Nodes.Clear(); //添加一個未分類和全部客戶的組別 TreeNode topNode = new TreeNode("所有記錄", 0, 0); this.treeView1.Nodes.Add(topNode); TreeNode CategoryNode = new TreeNode("客戶活動類別", 2, 2); this.treeView1.Nodes.Add(CategoryNode); AddDictData(CategoryNode, 0, "Category"); TreeNode OrderYearNode = new TreeNode("記錄年度", 8, 8); this.treeView1.Nodes.Add(OrderYearNode); List<string> yearList = BLLFactory<Maintenace>.Instance.GetYearList(); foreach (string year in yearList) { TreeNode subNode = new TreeNode(year, 0, 0); subNode.Tag = year; OrderYearNode.Nodes.Add(subNode); } this.treeView1.ExpandAll(); this.treeView1.EndUpdate(); }
為了處理樹形列表的節點的單擊事件,我們可以在其AfterSelect事件進行處理,示意代碼如下所示。主要邏輯就是根據及節點和條件的不同,進行不同的處理。
string treeConditionSql = ""; private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node != null && e.Node.Tag != null) { if (e.Node.FullPath.Contains("記錄年度")) { int year = Convert.ToInt32(e.Node.Tag.ToString()); SearchCondition condition = new SearchCondition(); condition.AddCondition("StartTime", Convert.ToDateTime(string.Format("{0}-01-01", year)), SqlOperator.MoreThanOrEqual); condition.AddCondition("StartTime", Convert.ToDateTime(string.Format("{0}-01-01", year + 1)), SqlOperator.LessThan); treeConditionSql = condition.BuildConditionSql().Replace("Where", ""); BindData(); } else { treeConditionSql = e.Node.Tag.ToString(); BindData(); } } else { treeConditionSql = ""; BindData(); } }
上面的代碼,我們定義了一個局部變量treeConditionSql 用來存儲樹列表單擊后的條件,觸發單擊事件后,我們最終還是傳回給標準列表界面已有的查詢操作--BindData函數進行處理。
BindData里面最主要的操作就是構造查詢條件,構造條件的語句如下所示,通過SearchCondition對象處理進行使用多數據庫的兼容處理。
/// <summary> /// 根據查詢條件構造查詢語句 /// </summary> private string GetConditionSql() { //如果存在高級查詢對象信息,則使用高級查詢條件,否則使用主表條件查詢 SearchCondition condition = advanceCondition; if (condition == null) { condition = new SearchCondition(); condition.AddCondition("Category", this.txtCategory.Text.Trim(), SqlOperator.Like); condition.AddCondition("Title", this.txtTitle.Text.Trim(), SqlOperator.Like); condition.AddDateCondition("StartTime", this.txtStartTime1, this.txtStartTime2); //日期類型 condition.AddCondition("Contact", this.txtContact.Text.Trim(), SqlOperator.Like); condition.AddCondition("Place", this.txtPlace.Text.Trim(), SqlOperator.Like); } string where = condition.BuildConditionSql().Replace("Where", ""); //如果是單擊節點得到的條件,則使用樹列表的,否則使用查詢條件的 if (!string.IsNullOrEmpty(treeConditionSql)) { where = treeConditionSql; } return where; }
最終綁定數據的函數BindData的邏輯代碼如下所示。
/// <summary> /// 綁定列表數據 /// </summary> private void BindData() { //entity this.winGridViewPager1.DisplayColumns = "Customer_ID,HandNo,Category,Title,Content,StartTime,EndTime,Contact,ContactPhone,ContactMobile,Place,PlaceAddress,PlacePhone,Note,Editor,EditTime"; this.winGridViewPager1.ColumnNameAlias = BLLFactory<Activity>.Instance.GetColumnNameAlias();//字段列顯示名稱轉義 string where = GetConditionSql(); List<ActivityInfo> list = BLLFactory<Activity>.Instance.FindWithPager(where, this.winGridViewPager1.PagerInfo); this.winGridViewPager1.DataSource = new WHC.Pager.WinControl.SortableBindingList<ActivityInfo>(list); this.winGridViewPager1.PrintTitle = "客戶活動管理報表"; }
這樣我們就完成了樹形列表和分頁查詢整合一起的數據查詢處理邏輯,從而實現我們說需要的結果,這樣的界面在某種程度上,給我們提供更多的方便,更好的體驗。
本文轉載自
DevExpress技術交流群3:700924826 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自: