显示DataTable的 TreeView 控件

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/geyunfei_hit/article/details/1924774

最近编程总要用到,所以简单封装了一个控件.

各参数说明如下,索引列表明生成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);

        }

    }

 

展开阅读全文

没有更多推荐了,返回首页