原創(chuàng)|使用教程|編輯:龔雪|2021-02-26 10:29:47.507|閱讀 298 次
概述:本文要介紹如何快速實(shí)現(xiàn)樹形列表和分頁查詢整合的Winform程序界面。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
DevExpress擁有.NET開發(fā)需要的所有平臺控件,包含600多個(gè)UI控件、報(bào)表平臺、DevExpress Dashboard eXpressApp 框架、適用于 Visual Studio的CodeRush等一系列輔助工具。
在做Winform界面的時(shí)候,一般都是統(tǒng)一化處理,界面頂部放置一些字段條件供查詢,下面就是分頁查詢列表,展示相關(guān)的數(shù)據(jù)。但有時(shí)候碰到一些表字段內(nèi)容分類比較多,有一些特別重要,如果放在一個(gè)樹形列表來進(jìn)行快速分類查詢,用戶體驗(yàn)應(yīng)該更好。本文要介紹如何快速實(shí)現(xiàn)樹形列表和分頁查詢整合的Winform程序界面。
標(biāo)準(zhǔn)的查詢條件+列表數(shù)據(jù)展示的WInform界面如下所示。
這個(gè)界面主要就是通過代碼生成工具()進(jìn)行初期的Winform界面生成即可。
以上的界面有時(shí)候感覺不夠友好,正如文章開頭說到,我需要在左邊放置一些重要的數(shù)據(jù)分類進(jìn)行查詢,這樣能夠提高用戶體驗(yàn)效果,最終希望的界面效果如下所示。
為了實(shí)現(xiàn)這種效果,我們需要進(jìn)行幾部操作。
在標(biāo)準(zhǔn)列表界面上增加窗口分割控件(如DevExpress的是SplitContainerControl控件)
傳統(tǒng)的Winform界面可以使用SplitContainer控件
在現(xiàn)有已生成界面的基礎(chǔ)上,把查詢部分和列表部分的控件拖動小一點(diǎn),然后把上述分隔控件拖動到界面后,在右邊面板放入已有的查詢和分頁控件部分的內(nèi)容,中間狀態(tài)的列表界面效果如下所示。
然后在左邊放入一個(gè)GroupControl控件,并加入樹形控件TreeView,這樣我們調(diào)整后的設(shè)計(jì)界面效果如下所示。
首先我們需要在代碼里面綁定樹的初始化代碼,生成需要快速查詢的內(nèi)容,示意代碼如下所示。主要邏輯思路就是,從數(shù)據(jù)字典中檢索相關(guān)的分類,然后綁定一些查詢條件,方便后面的處理。
private void InitTree() { base.LoginUserInfo = Cache.Instance["LoginUserInfo"] as LoginUserInfo; this.treeView1.BeginUpdate(); this.treeView1.Nodes.Clear(); //添加一個(gè)未分類和全部客戶的組別 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(); }
為了處理樹形列表的節(jié)點(diǎn)的單擊事件,我們可以在其AfterSelect事件進(jìn)行處理,示意代碼如下所示。主要邏輯就是根據(jù)及節(jié)點(diǎn)和條件的不同,進(jìn)行不同的處理。
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(); } }
上面的代碼,我們定義了一個(gè)局部變量treeConditionSql 用來存儲樹列表單擊后的條件,觸發(fā)單擊事件后,我們最終還是傳回給標(biāo)準(zhǔn)列表界面已有的查詢操作--BindData函數(shù)進(jìn)行處理。
BindData里面最主要的操作就是構(gòu)造查詢條件,構(gòu)造條件的語句如下所示,通過SearchCondition對象處理進(jìn)行使用多數(shù)據(jù)庫的兼容處理。
/// <summary> /// 根據(jù)查詢條件構(gòu)造查詢語句 /// </summary> private string GetConditionSql() { //如果存在高級查詢對象信息,則使用高級查詢條件,否則使用主表?xiàng)l件查詢 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", ""); //如果是單擊節(jié)點(diǎn)得到的條件,則使用樹列表的,否則使用查詢條件的 if (!string.IsNullOrEmpty(treeConditionSql)) { where = treeConditionSql; } return where; }
最終綁定數(shù)據(jù)的函數(shù)BindData的邏輯代碼如下所示。
/// <summary> /// 綁定列表數(shù)據(jù) /// </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();//字段列顯示名稱轉(zhuǎn)義 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 = "客戶活動管理報(bào)表"; }
這樣我們就完成了樹形列表和分頁查詢整合一起的數(shù)據(jù)查詢處理邏輯,從而實(shí)現(xiàn)我們說需要的結(jié)果,這樣的界面在某種程度上,給我們提供更多的方便,更好的體驗(yàn)。
本文轉(zhuǎn)載自
DevExpress技術(shù)交流群3:700924826 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: