最近編程總要用到,所以簡單封裝了一個控件.
各參數說明如下,索引列表明生成tree結構的索引,而級別表示第幾級,每級編碼長度是一個輔助值,如
1,2,3級編碼長度為2,1,3那麼對索引列1,101,102,101001,101002,101003生成的樹如下
1的子結點為101,102,101的子結點為101001,101002,101003
程序中已經將TreeVIEw 的MouseClick事件甩出
而且,對於dataTable的每一列,都存在對應的TNode 的Tag中,應用時只要轉換一下就行了
感覺封裝性不是太好
歡迎拍磚
public delegate void NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e);
public partial class CertiRuleVIEw : UserControl
...{
private String _indexCol; //索引列
private String _levelCol; //級別列
private String _showCol; //顯示列
private String _valueCol; //值列
private String _levelLengthCol;//該級編碼長度
private DataTable _table; //數據表
private NodeMouseClick _mouseClick; //鼠標事件
/**//// <summary>
/// MouseClick 事件,可以將單擊結點的事件傳出
/// </summary>
public new event NodeMouseClick MouseClick
...{
add
...{
this._mouseClick += new NodeMouseClick(value);
}
remove
...{
this._mouseClick -= new NodeMouseClick(value);
}
}
/**//// <summary>
/// 表示各級編碼長度的列
/// </summary>
public String LevelLengthCol
...{
get
...{
return _levelLengthCol;
}
set
...{
if ( _table==null || _table.Columns.Contains(value))
_levelLengthCol = value;
else
throw new Exception("要設定的列不存在!");
}
}
/**//// <summary>
/// 要顯示的表
/// </summary>
public DataTable Table
...{
get
...{
return _table;
}
set
...{
_table = value;
}
}
/**//// <summary>
/// 表的索引列
/// </summary>
public String IndexCol
...{
get
...{
return _indexCol;
}
set
...{
if (_table==null ||_table.Columns.Contains(value))
_indexCol = value;
else
throw new Exception("給定的列並不存在!");
}
}
/**//// <summary>
/// 表示級別的列
/// </summary>
public String LevelCol
...{
get
...{
return _levelCol;
}
set
...{
if (_table == null || _table.Columns.Contains(value))
_levelCol = value;
else
throw new Exception("給定的列並不存在!");
}
}
/**//// <summary>
/// 要顯示在結點上的列
/// </summary>
public String ShowCol
...{
get
...{
return _showCol;
}
set
...{
if (_table == null || _table.Columns.Contains(value))
_showCol = value;
else
throw new Exception("給定的列並不存在!");
}
}
/**//// <summary>
/// 用來索引的列
/// </summary>
public String ValueCol...{
get...{
return _valueCol;
}
set...{
if (_table == null || _table.Columns.Contains(value ))
_valueCol = value;
else
throw new Exception ("給定的列並不存在!");
}
}
/**//// <summary>
/// 得到TreeVIEw的實例
/// </summary>
public TreeVIEw Tree
...{
get
...{
return treeVIEw1;
}
}
public CertiRuleVIEw()
...{
InitializeComponent();
}
/**//// <summary>
/// 初始化結點
/// </summary>
public void Init()
...{
DataRow[] listRows = _table.Select(null, _indexCol + " ASC");
TreeNodeCollection p = this.treeVIEw1.Nodes;
TreeNode node;
TreeNode current;
String key;
int length;
int index;
List<TreeNode> T;
T = new List<TreeNode>();
foreach (DataRow row in listRows)
...{
node = new TreeNode(row[_showCol].ToString()+":"+row[_valueCol].ToString());
node.Tag = row;
key = row[_indexCol].ToString();
node.Name = key;
length = System.Convert.ToInt32(row[_levelLengthCol].ToString());
key = key.Substring(0, key.Length - length);
if (treeVIEw1.Nodes.Count == 0)
treeVIEw1.Nodes.Add(node);
else...{
foreach (TreeNode childnode in this.treeVIEw1.Nodes)
...{
T.Add(childnode);
}
while (T.Count > 0)
...{
current = T[0];
T.Remove(current);
if (current.Name == key)
...{
current.Nodes.Add(node);
node = null;
break;
}
else
...{
if(current.Nodes.Count > 0)
foreach (TreeNode child in current.Nodes)
...{
T.Add(child);
}
}
}
if(node != null)
treeVIEw1.Nodes.Add(node);
}
}
}
private void treeVIEw1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
...{
if (_mouseClick != null)
_mouseClick(sender, e);
}
}