代码改变世界

c#,winform,webform,treeview,无限级自动生成

2008-07-01 14:51  Virus-BeautyCode  阅读(8631)  评论(9编辑  收藏  举报


 

16 AuthorizeMenuInfo权限菜单表

 

列名

类型

大小

说明

MenuID

Int

4

菜单ID

MenuName

Varchar

50

菜单名称

ParentID

Int

4

为此菜单的父菜单ID如果是顶级菜单则为0

FormName

Varchar

50

为单击菜单所要显示的窗体类名如果是还有下级菜单的则不要写。

IsModule

Varchar

1

是否还有子菜单,1有,0没有

Level

Int

4

层数

Remark

Varchar

200

备注

 

 

 

 

 

 

 

 

 


存储过程
/*
查询构造treeview所需信息
*/
create
 procedure GetTreeViewAuthorize
as
select MenuID,MenuName,ParentID from AuthorizeMenuInfo
go


/*
根据父节点ID查找子节点信息
*/
create procedure GetAuthorizeByParentID
@ParentID int
as
select * from AuthorizeMenuInfo where ParentID=@ParentID
go


/// <summary>
        
/// 获取构建无限级treeview所需的菜单id,菜单名称,父菜单id
        
/// 返回datatable
        
/// </summary>
        
/// <returns>无限级treeview所需datatable</returns>

        public DataTable GetTreeViewAuthorize()
        
{
            DataTable dt 
= new DataTable();

            SqlConnection conn 
= new SqlConnection(connectionString);
            SqlCommand cmd 
= new SqlCommand();
            cmd.Connection 
= conn;
            cmd.CommandText 
= "GetTreeViewAuthorize";
            cmd.CommandType 
= CommandType.StoredProcedure;

            
try
            
{
                conn.Open();
                
using (SqlDataReader reader = cmd.ExecuteReader())
                
{
                    
if(reader!=null)

                        dt.Load(reader);
                   
                }

            }

            
catch
            
{
                
throw;
            }

            
finally
            
{
                conn.Close();
            }

            
return dt;
        }

/// <summary>
        
/// 根据父节点ID查找子节点信息
        
/// </summary>
        
/// <param name="parentID">父节点ID</param>
        
/// <returns>子节点信息</returns>

        public DataTable GetAuthorizeByParentID(int parentID)
        
{
            DataTable dt 
= new DataTable();

            SqlConnection conn 
= new SqlConnection(connectionString);
            SqlCommand cmd 
= new SqlCommand();
            cmd.Connection 
= conn;
            cmd.CommandText 
= "GetAuthorizeByParentID";
            cmd.Parameters.AddWithValue(
"@ParentID", parentID);
            cmd.CommandType 
= CommandType.StoredProcedure;

            
try
            
{
                conn.Open();
                
using (SqlDataReader reader = cmd.ExecuteReader())
                
{
                    
if (reader != null)

                        dt.Load(reader);

                }

            }

            
catch
            
{
                
throw;
            }

            
finally
            
{
                conn.Close();
            }

            
return dt;

        }


/// <summary>
/// 构造权限treeview
/// </summary>

        private void fill_tvAuthList()
        
{
            DataTable dt 
= new DataTable();
            BLLAuthorize bllAuthorize 
= new BLLAuthorize();
            dt 
= bllAuthorize.GetTreeViewAuthorize();
            DataRow[] rows 
= dt.Select("[ParentID]=0");
            
for(int i=0;i<rows.GetUpperBound(0);i++)
            
{
                TreeNode treeNode 
= new TreeNode();
                treeNode.Name 
= rows[i]["MenuID"].ToString();
                treeNode.Text 
= rows[i]["MenuName"].ToString();
                
                DataRow[] rows1 
= dt.Select("[ParentID]=" + Convert.ToInt32(rows[i]["MenuID"]));
                
if (rows1.GetUpperBound(0> -1)
                
{
                    
foreach (DataRow row in rows1)
                    
{
                        TreeNode node 
= new TreeNode();
                        node.Name 
= row["MenuID"].ToString();
                        node.Text 
= row["MenuName"].ToString();
                        treeNode.Nodes.Add(node);
                    }

                }

tvAuthList.Nodes.Add(treeNode);
            }

        }

        
private void fill_tvAuthListNode(TreeNode treeNode)
        
{
           
// TreeNode[]nodes=new TreeNode[];
            DataTable dt = new DataTable();
            BLLAuthorize bllAuthorize 
= new BLLAuthorize();
            dt 
= bllAuthorize.GetAuthorizeByParentID(Convert.ToInt32(treeNode.Name));

            
for (int i = 0; i < dt.Rows.Count; i++)
            
{
                TreeNode tNode 
= new TreeNode();
                tNode.Name 
= dt.Rows[i]["MenuID"].ToString();
                tNode.Text 
= dt.Rows[i]["MenuName"].ToString();
                treeNode.Nodes.Add(tNode);
            }

        }

        
private void AddSysRoleForm_Load(object sender, EventArgs e)
        
{
            fill_tvAuthList();
        }