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);
}
}
}
/// <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);
}
}
}
相关文章推荐
- C#之Winform中treeview控件绑定数据库
- TreeView树形控件递归绑定数据库里的数据
- C#里的treeview控件如何能绑定数据库里的数据
- 关于VS2005中TreeView控件动态绑定数据库
- 树形控件TreeView与数据库绑定
- 母版页 treeview控件 SiteMapPath控件 treeview数据库绑定模式
- TreeView控件数据库绑定
- TreeView控件绑定数据库
- 我想用 TreeView控件来绑定数据库中的产品(产品在数据库中有三级),我不会绑定!!哪位高人可以告诉我怎么写?
- C# TreeView控件动态绑定数据库
- C#之Winform中treeview控件绑定数据库
- DX控件中TreeView绑定层次数据库数据
- TreeView(树控件)与数据库绑定
- C# TreeView树视图控件 如何把树与数据库的信息进行绑定
- DX控件中TreeView绑定层次数据库数据
- 快速开发框架Afinal的使用(数据库操作,HTTP请求,网络图片加载,控件绑定)
- asp.net中将数据库绑定到DataList控件的实现方法与实例代码
- c# ComboBox 控件 用数据库填充 绑定
- 利用TreeView控件 从数据库中动态载入数据
- ASP.NET2.0 TreeView的数据库绑定