zoukankan      html  css  js  c++  java
  • DropDownList分层显示!

    public static void BindDropFatherItem( DropDownList DropDownList )
        
    {
            DropDownList.Items.Clear();
            
    string strSql = "select * from Department";
            DataTable dt 
    = DB.GetTable( strSql );//获取所有节点

            
    //判断跟节点数量
            string strSql1 = "select count(*) from Department where  ParentID = 0";
            
    int ParentCount = Convert.ToInt32( DB.ExecuteScalar( strSql1 ) );

            
    if( dt.Rows.Count > 0 )
            
    {
                
    foreach( DataRow dr in dt.Rows )
                
    {
                    
    if( ParentCount == 0 )
                    
    {
                        
    if( dr[ "ParentID" ].ToString().Trim() == "0" )//绑定根节点   
                        {
                            DropDownList.Items.Insert( 
    0"请选择" );
                            DropDownList.Items.Add( 
    new ListItem( dr[ "DepName" ].ToString(), dr[ "DepID" ].ToString() ) );
                            BindDropChildItem( DropDownList, dt, dr[ 
    "DepID" ].ToString(), 1 );
                        }

                    }

                    
    else
                    
    {
                        
    if( dr[ "ParentID" ].ToString().Trim() == "0" )//绑定根节点   
                        {                        
                            DropDownList.Items.Add( 
    new ListItem( dr[ "DepName" ].ToString(), dr[ "DepID" ].ToString() ) );
                            BindDropChildItem( DropDownList, dt, dr[ 
    "DepID" ].ToString(), 1 );
                        }

                    }

                    
                }

            }

        }

    public static void BindDropChildItem( DropDownList DropDownList, DataTable dt, string id, int length )
        
    {
            DataRow[] rows 
    = dt.Select( "ParentID='" + id + "'""DepID  ASC" );//取出id子节点进行绑定   
            forint i = 0; i < rows.Length; i++ )
            
    {
                DropDownList.Items.Add( 
    new ListItem( Department.SpaceLength( length ) + rows[ i ][ "DepName" ].ToString(), rows[ i ][ "DepID" ].ToString() ) );
                BindDropChildItem( DropDownList, dt, rows[ i ][ 
    "DepID" ].ToString(), length + 1 );//空白数目加1   
            }

        }


        
    //   子节点前面的空白数 
        public static string SpaceLength( int i )
        
    {
            
    string space = "";
            
    forint j = 0; j < i; j++ )
            
    {
                space 
    += "------";//分层显示字符;   
            }

            
    return space;
        }
        
  • 相关阅读:
    Problem E. Matrix from Arrays(杭电2018年多校第四场+思维+打表找循环节)
    Reachability from the Capital(Codeforces Round #490 (Div. 3)+tarjan有向图缩点)
    Network of Schools(POJ1326+有向图进行缩点)
    John's trip(POJ1041+欧拉回路+打印路径)
    Watchcow(POJ2230+双向欧拉回路+打印路径)
    Network(POJ3694+边双连通分量+LCA)
    Problem L. Visual Cube(杭电多校2018年第三场+模拟)
    floyd骚操作——传递闭包
    Remmarguts' Date(POJ2449+最短路+A*算法)
    Transformation(线段树+HDU4578+多种操作+鬼畜的代码)
  • 原文地址:https://www.cnblogs.com/Magicam/p/1215616.html
Copyright © 2011-2022 走看看