我在作Winform界面的時候,通常都是統一化處理,界面頂部放置一些字段條件供查詢,下面就是分頁查詢列表,展現相關的數據。但有時候碰到一些表字段內容分類比較多,有一些特別重要,若是放在一個樹形列表來進行快速分類查詢,用戶體驗應該更好。本篇隨筆主要介紹如何快速實現樹形列表和分頁查詢整合的WInform程序界面。html
標準的查詢條件+列表數據展現的WInform界面以下所示。數據庫
這個界面主要就是經過代碼生成工具(Database2Sharp)進行初期的Winform界面生成便可。要了解具體如何生成標準的列表界面和數據編輯界面,查看下面兩篇操做介紹隨筆便可。函數
《利用代碼生成工具Database2Sharp設計數據編輯界面》、《代碼生成工具之Winform查詢列表界面生成》。工具
以上的界面有時候感受不夠友好,正如文章開頭說到,我須要在左邊放置一些重要的數據分類進行查詢,這樣可以提升用戶體驗效果,最終但願的界面效果以下所示。post
爲了實現這種效果,咱們須要進行幾部操做。ui
1)在標準列表界面上增長窗口分割控件(如DevExpress的是SplitContainerControl控件)this
傳統的Winform界面可使用SplitContainer控件url
在現有已生成界面的基礎上,把查詢部分和列表部分的控件拖動小一點,而後把上述分隔控件拖動到界面後,在右邊面板放入已有的查詢和分頁控件部分的內容,中間狀態的列表界面效果以下所示。spa
而後在左邊放入一個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 = "客戶活動管理報表"; }
這樣咱們就完成了樹形列表和分頁查詢整合一塊兒的數據查詢處理邏輯,從而實現咱們說須要的結果,這樣的界面在某種程度上,給咱們提供更多的方便,更好的體驗。