C#里的treeview控件如何能绑定数据库里的数据
2009-09-25 11:15
701 查看
应用程序,窗体里拖放一个TreeView控件,想把数据库里的数据在这个控件里显示出来
TREEVIEW控件是Winfrom窗体里的 不是ASP.NET里的TREEVIEW
代码如下:
CREATE TABLE CateTable (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[CateName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[RootID] [int] NOT NULL ,
[ParentID] [int] NOT NULL
)
GO
存储过程:
CREATE PROCEDURE CateTable_GetList AS
BEGIN
Select
CateID,
CateName,
RootID,
ParentID
From
CateTable
Order By
CateID,RootID,ParentID
END
代码:
private void MainForm_Load(object sender, EventArgs e)
{
//从数据库中读取数据
SqlConnection con = new SqlConnection(ConnString.ConStr);
SqlCommand cmd = new SqlCommand("CateTable_GetList", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
sda.Fill(ds);
}
catch
{
}
finally
{
cmd = null;
con.Close();
}
//往TreeView中添加树节点
//添加根节点
TreeNode tn = new TreeNode();
tn.Text = "种类";
tn.Name = "0";//Name作为ID
tn.Tag = "0";//Tag作为RootID
tn.ImageIndex = 0;
tn.SelectedImageIndex = 0;
tv.Nodes.Add(tn);//该TreeView命名为tv
tv.SelectedNode = tv.TopNode;
//把其他节点加上去
if (ds != null)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
tn = new TreeNode();
tn.Text = dr["CateName"].ToString();
tn.Name = dr["CateID"].ToString();//Name作为CateID
tn.Tag = dr["RootID"].ToString();//Tag作为RootID
tn.ImageIndex = 1;
tn.SelectedImageIndex = 1;
//判断是否为主节点
if (dr["CateID"].ToString() == dr["RootID"].ToString())
{
//主节点
tv.SelectedNode = tv.TopNode;
}
else
{
//其他节点
if (tv.SelectedNode.Name != dr["ParentID"].ToString())
{
TreeNode[] tn_temp = tv.Nodes.Find(dr["ParentID"].ToString(), true);
if (tn_temp.Length > 0)
{
tv.SelectedNode = tn_temp[0];
}
}
}
tv.SelectedNode.Nodes.Add(tn);
}
tv.ExpandAll();//展开TreeView
tv.SelectedNode = tv.TopNode;
}
}
TREEVIEW控件是Winfrom窗体里的 不是ASP.NET里的TREEVIEW
代码如下:
CREATE TABLE CateTable (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[CateName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[RootID] [int] NOT NULL ,
[ParentID] [int] NOT NULL
)
GO
存储过程:
CREATE PROCEDURE CateTable_GetList AS
BEGIN
Select
CateID,
CateName,
RootID,
ParentID
From
CateTable
Order By
CateID,RootID,ParentID
END
代码:
private void MainForm_Load(object sender, EventArgs e)
{
//从数据库中读取数据
SqlConnection con = new SqlConnection(ConnString.ConStr);
SqlCommand cmd = new SqlCommand("CateTable_GetList", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
sda.Fill(ds);
}
catch
{
}
finally
{
cmd = null;
con.Close();
}
//往TreeView中添加树节点
//添加根节点
TreeNode tn = new TreeNode();
tn.Text = "种类";
tn.Name = "0";//Name作为ID
tn.Tag = "0";//Tag作为RootID
tn.ImageIndex = 0;
tn.SelectedImageIndex = 0;
tv.Nodes.Add(tn);//该TreeView命名为tv
tv.SelectedNode = tv.TopNode;
//把其他节点加上去
if (ds != null)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
tn = new TreeNode();
tn.Text = dr["CateName"].ToString();
tn.Name = dr["CateID"].ToString();//Name作为CateID
tn.Tag = dr["RootID"].ToString();//Tag作为RootID
tn.ImageIndex = 1;
tn.SelectedImageIndex = 1;
//判断是否为主节点
if (dr["CateID"].ToString() == dr["RootID"].ToString())
{
//主节点
tv.SelectedNode = tv.TopNode;
}
else
{
//其他节点
if (tv.SelectedNode.Name != dr["ParentID"].ToString())
{
TreeNode[] tn_temp = tv.Nodes.Find(dr["ParentID"].ToString(), true);
if (tn_temp.Length > 0)
{
tv.SelectedNode = tn_temp[0];
}
}
}
tv.SelectedNode.Nodes.Add(tn);
}
tv.ExpandAll();//展开TreeView
tv.SelectedNode = tv.TopNode;
}
}
相关文章推荐
- C# TreeView树视图控件 如何把树与数据库的信息进行绑定
- C# TreeView从数据库绑定数据
- C# treeview 控件动态绑定数据
- 如何将数据库中的字段数据绑定显示在Label或TextBox控件中
- TreeView如何与数据库二维表数据绑定
- C# TreeView控件动态绑定数据库
- C#中,datagridview与sql数据源绑定后,通过控件更改数据如何自动回写更新到数据源
- C#之Winform中treeview控件绑定数据库
- 如何使数据绑定的控件实现“上一条”、“下一条”、“第一条”和“最后一条”的功能?(WinForm, C#)
- DX控件中TreeView绑定层次数据库数据
- C# 程序中如何向(DEV)ComboBoxEdit控件中添加下拉列表的值不使用数据绑定,就是使用代码添加
- 关于TreeView绑定数据库的表数据如何,代码在前两篇博文中~~
- c# 数据库数据与DataGridView表控件的绑定
- 关于如何递归给TreeView从数据库中循环绑定数据
- TreeView树形控件递归绑定数据库里的数据
- c# 如何向comboBox中绑定数据库中指定的数据信息;
- C# .NET treeView(树形控件)绑定数据的通用方法 (精)
- C#之Winform中treeview控件绑定数据库
- DX控件中TreeView绑定层次数据库数据