欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解在DevExpress程序中使用TreeList控件以及節(jié)點查詢的處理

 更新時間:2016年12月26日 08:48:07   作者:伍華聰  
本篇文章主要介紹基于DevExpress的TreeList控件使用以及使用SearchControl對節(jié)點進(jìn)行查詢的操作,具有一定的參考價值,下面跟著小編一起來看下吧

在很多情況下,我們需要通過樹列表進(jìn)行數(shù)據(jù)的展示,如一些有層次關(guān)系的數(shù)據(jù),通過有層級的展示,能夠使用戶更加直觀查看和管理相關(guān)的數(shù)據(jù)。在一般Winform開發(fā)的情況下,可以使用微軟的TreeView控件,也可以使用DevExpress的TreeList控件進(jìn)行數(shù)據(jù)的展示,本篇隨筆主要介紹基于DevExpress的TreeList控件使用以及使用SearchControl對節(jié)點進(jìn)行查詢的操作。

1、 使用微軟的TreeView控件的實現(xiàn)效果和思路

在很多情況下,我們也傾向于使用TreeView控件作為數(shù)據(jù)的展示,相對于TreeList控件,這種控件的處理,需要自己管理樹節(jié)點的層次關(guān)系,不過使用也比較簡單,呈現(xiàn)的效果兩者都差別不大。

如在我開發(fā)框架中,在字典管理模塊里面,就是采用這個控件進(jìn)行數(shù)據(jù)的展示的,整體效果也還不錯。

在樹形列表里面,我們獲取數(shù)據(jù)后,統(tǒng)一根據(jù)層級的關(guān)系構(gòu)建樹節(jié)點即可,如下代碼所示。

/// <summary>
/// 初始化樹信息
/// </summary>
private void InitTreeView()
{
  this.treeView1.Nodes.Clear();
  this.treeView1.BeginUpdate();
  List<DictTypeNodeInfo> typeNodeList = BLLFactory<DictType>.Instance.GetTree();
  foreach (DictTypeNodeInfo info in typeNodeList)
  {
    AddTree(null, info);
  }
  this.treeView1.EndUpdate();
  this.treeView1.ExpandAll();
}
/// <summary>
/// 根據(jù)節(jié)點數(shù)據(jù),遞歸構(gòu)建該層級以下的樹節(jié)點
/// </summary>
/// <param name="pNode">父樹節(jié)點</param>
/// <param name="info">字典類型數(shù)據(jù)</param>
private void AddTree(TreeNode pNode, DictTypeNodeInfo info)
{
  TreeNode node = null;
  if (info.PID == "-1")
  {
    node = new TreeNode(info.Name, 0, 0);
    node.Tag = info.ID;
    this.treeView1.Nodes.Add(node);
  }
  else
  {
    node = new TreeNode(info.Name, 1, 1);
    node.Tag = info.ID;
    pNode.Nodes.Add(node);
  }
  foreach (DictTypeNodeInfo subInfo in info.Children)
  {
    AddTree(node, subInfo);
  }
}

還有我們在鼠標(biāo)選擇某個節(jié)點的時候,觸發(fā)AfterSelect事件,這樣我們就可以處理鼠標(biāo)節(jié)點的事件了

/// <summary>
/// 單擊節(jié)點事件處理
/// </summary>
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
  if (e.Node.Tag != null)
  {
    this.lblDictType.Text = e.Node.Text;
    this.lblDictType.Tag = e.Node.Tag;
    BindData();
  }
}

以上就是使用TreeView控件來處理數(shù)據(jù)的展示,從上面代碼可以看到,整體的內(nèi)容,主要是通過遞歸的關(guān)系來構(gòu)建TreeNode的處理,但是使用的代碼也不算復(fù)雜,因此大多數(shù)可以采用這種方式來自定義樹形節(jié)點的展示。

2、使用DevExpress的TreeList控件的效果和實現(xiàn)代碼

而使用DevExpress的TeeList控件,可以通過KeyFieldName和ParentFieldName指定他們的層級關(guān)系,使用就更加簡單化,提供的數(shù)據(jù)源會自動進(jìn)行層次的關(guān)系處理,非常方便。

我們先來看看通過DevExpress的TreeList控件展示的字典類型和字典數(shù)據(jù)的界面效果。

這里面的效果是如何通過代碼實現(xiàn)的呢?

首先我們使用代碼獲取字典類型數(shù)據(jù)并進(jìn)行樹列表控件的初始化操作,如下所示。

//添加顯示列
this.tree.Columns.Add(new TreeListColumn{ FieldName= "Name", Caption= "字典類型名稱", Width=160, VisibleIndex =0});
//設(shè)置樹控件的層次關(guān)系及屬性
tree.KeyFieldName = "ID";
tree.ParentFieldName = "PID";
this.tree.OptionsBehavior.Editable = false;
this.tree.OptionsView.EnableAppearanceOddRow = true;
this.tree.OptionsView.EnableAppearanceEvenRow = true;

上面的代碼我們還可以通過擴(kuò)展函數(shù)對樹列表的處理進(jìn)行封裝,已達(dá)到簡化代碼的目的,如下是處理后的實現(xiàn)代碼:

//控件擴(kuò)展函數(shù)封裝處理
this.tree.CreateColumn("Name", "字典類型名稱", 160, true);
this.tree.InitTree("ID", "PID", "-1", false, false);

通過添加TreeListColumn對象給TreeList控件就可以實現(xiàn)字段列的顯示了,同時指定數(shù)據(jù)源里面的KeyFieldName和ParentFieldName來設(shè)定層級關(guān)系即可,非常簡單。

而綁定數(shù)據(jù)源,則可以通過一個函數(shù)進(jìn)行處理,如下所示。

/// <summary>
/// 綁定樹的數(shù)據(jù)源
/// </summary>
private void BindTree()
{
  this.tree.DataSource = BLLFactory<DictType>.Instance.GetAll();
  this.tree.ExpandAll();
}

從上面代碼我們可以看到,我們返回的數(shù)據(jù)源,不需要在實體類對象層級具有上下級的關(guān)系,如通過TreeView實現(xiàn)的時候,我們使用了DictTypeNodeInfo 對象是具有上下層級關(guān)系的。

這里只需要使用普通的DictTypeInfo 對象集合即可,通過KeyFieldName和ParentFieldName來設(shè)定層級關(guān)系即可。

為了指定樹形節(jié)點的圖標(biāo),我們可以通過代碼進(jìn)行自定義圖標(biāo)的處理,如下代碼所示,這樣每個層級的圖標(biāo)都不一樣,自動實現(xiàn)獲取設(shè)置的處理。

//設(shè)置樹的圖標(biāo)集合及逐級圖標(biāo)
this.tree.SelectImageList = this.imageCollection1;
this.tree.CustomDrawNodeImages += (object sender, CustomDrawNodeImagesEventArgs e)=>
{
  int maxCount = this.imageCollection1.Images.Count;
  var index = e.Node.Level < maxCount ? e.Node.Level : 0;
  e.SelectImageIndex = index;
};

實現(xiàn)樹節(jié)點選中的事件處理,則需要實現(xiàn)FocusedNodeChanged事件即可。

//初始化樹節(jié)點選擇事件
  this.tree.FocusedNodeChanged += delegate(object sender, FocusedNodeChangedEventArgs e)
  {
    this.FocusedNodeChanged();
  };
}
private void FocusedNodeChanged()
{
  if (this.tree.FocusedNode != null)
  {
    var PID = string.Concat(this.tree.FocusedNode.GetValue("ID"));
    treeConditionSql = string.Format("DictType_ID = '{0}'", PID);
  }
  else
  {
    treeConditionSql = "";
  }
  BindData();
}

最后初始化樹列表的代碼如下所示。

private void InitTree()
{
  this.tree.Columns.Clear();
  //控件擴(kuò)展函數(shù)封裝處理
  this.tree.CreateColumn("Name", "字典類型名稱", 160, true);
  this.tree.InitTree("ID", "PID", "-1", false, false);
  //設(shè)置樹的圖標(biāo)集合及逐級圖標(biāo)
  this.tree.SelectImageList = this.imageCollection1;
  this.tree.CustomDrawNodeImages += (object sender, CustomDrawNodeImagesEventArgs e)=>
  {
    int maxCount = this.imageCollection1.Images.Count;
    var index = e.Node.Level < maxCount ? e.Node.Level : 0;
    e.SelectImageIndex = index;
  };
}

3、基于SearchControl控件對節(jié)點進(jìn)行查詢的操作

上面的處理就是樹列表的一般性展示,如果需要在樹節(jié)點上面增加一個查詢過濾的操作,那么可以使用SearchControl控件進(jìn)行過濾處理,只需要設(shè)置SearchControl控件的Client屬性,以及實現(xiàn)樹控件的FilterNode事件即可。

/// <summary>
/// 實現(xiàn)樹節(jié)點的過濾查詢
/// </summary>
private void InitSearchControl()
{
  this.searchControl1.Client = this.tree;
  this.tree.FilterNode += (object sender, DevExpress.XtraTreeList.FilterNodeEventArgs e) =>
  {
    if (tree.DataSource == null)
      return;
    string nodeText = e.Node.GetDisplayText("Name");//參數(shù)填寫FieldName 
    if (string.IsNullOrWhiteSpace(nodeText))
      return;
    bool isExist = nodeText.IndexOf(searchControl1.Text, StringComparison.OrdinalIgnoreCase) >= 0;
    if (isExist)
    {
      var node = e.Node.ParentNode;
      while (node != null)
      {
        if (!node.Visible)
        {
          node.Visible = true;
          node = node.ParentNode;
        }
        else
          break;
      }
    }
    e.Node.Visible = isExist;
    e.Handled = true;
  };
}

實現(xiàn)效果如下所示, 對于符合記錄的查詢,那么會有高亮的顏色進(jìn)行重點標(biāo)注。

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

最新評論