您的位置:首页 > 数据库

TreeView控件绑定多个数据库表

2006-11-24 15:42 330 查看
一.创建方法

/// <summary>
/// 填充数据
/// </summary>
/// <param name="ds">绑定数据</param>
/// <param name="tableName">表名</param>
/// <param name="strSql">SQL查寻语句参数</param>
public void FillData(DataSet ds, string tableName, string strSql)
{
ds.Clear();
SqlConnection conn = new SqlConnection(SqlHelper.ConnectionString.ToString());
conn.Open();
SqlDataAdapter myAdapter = new SqlDataAdapter(strSql, conn);
myAdapter.Fill(ds, tableName);
}

二..绑定树

/// <summary>
/// 建立目录、模块、功能树
/// </summary>
/// <param name="tvCatModFunTree">TreeView的对象</param>
public void InitTree(TreeView tvCatModFunTree)
{
//填充数据集:dsCatalogManager
DataSet dsCatalogManager = new DataSet();
string strSqlCatalogManager = "select CatalogId,CatalogName from CatalogManager where Active='Y' order by CatalogId";
FillData(dsCatalogManager, "CatalogManager", strSqlCatalogManager);

if (dsCatalogManager.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < dsCatalogManager.Tables[0].Rows.Count; i++)
{
//把目录表绑定到根节点,作为第一级节点
TreeNode tnCatalogManager = new TreeNode();
tnCatalogManager.Value = dsCatalogManager.Tables[0].Rows[i]["CatalogId"].ToString();
tnCatalogManager.Text = dsCatalogManager.Tables[0].Rows[i]["CatalogName"].ToString();
tnCatalogManager.SelectAction = TreeNodeSelectAction.None;//选定节点,事件为空

//填充数据集:dsModuleManager
DataSet dsModuleManager = new DataSet();
string strSqlModuleManager = "select ModuleID,ModuleName,CatalogID from ModuleManager where Active='Y' and catalogid='" + dsCatalogManager.Tables[0].Rows[i][0].ToString().Trim() + "' order by ModuleID ";
FillData(dsModuleManager, "ModuleManager", strSqlModuleManager);

if (dsModuleManager.Tables[0].Rows.Count > 0)
{
for (int j = 0; j < dsModuleManager.Tables[0].Rows.Count; j++)
{
//把模块表绑定到子节点,作为第二级节点
TreeNode tnModuleName = new TreeNode();
tnModuleName.Value = dsModuleManager.Tables[0].Rows[j]["ModuleID"].ToString();
tnModuleName.Text = dsModuleManager.Tables[0].Rows[j]["ModuleName"].ToString();
tnModuleName.SelectAction = TreeNodeSelectAction.None; //选定节点,事件为空

//填充数据集:dsFunctionManager
DataSet dsFunctionManager = new DataSet();
string strSqlFunctionManager = "select FunctionID,FunctionName,ModuleID from FunctionManager where Active='Y' and ModuleID='"
+ dsModuleManager.Tables[0].Rows[j][0].ToString().Trim() + "' order by FunctionID ";
FillData(dsFunctionManager, "FunctionManager", strSqlFunctionManager);

if (dsFunctionManager.Tables[0].Rows.Count > 0)
{
for (int k = 0; k < dsFunctionManager.Tables[0].Rows.Count; k++)
{
//把功能表绑定到叶子节点,作为第三级节点
TreeNode tnFunctionName = new TreeNode();
tnFunctionName.Value = dsFunctionManager.Tables[0].Rows[k]["FunctionID"].ToString();
tnFunctionName.Text = dsFunctionManager.Tables[0].Rows[k]["FunctionName"].ToString();
tnFunctionName.Selected = false;
tnFunctionName.SelectAction = TreeNodeSelectAction.None;//选定节点,事件为空

//把功能名称绑定到模块功能名称下,作为尾节点
tnModuleName.ChildNodes.Add(tnFunctionName);
}
}
//把模块名称绑定到目录名称下,作为子节点
tnModuleName.ExpandAll();
tnCatalogManager.ChildNodes.Add(tnModuleName);
}
}
// 把目录名称绑定到TreeView控件,作为父节点
tnCatalogManager.ExpandAll();
tvCatModFunTree.Nodes.Add(tnCatalogManager);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: