VS2005中,dataset数据如何显示到menu和treeview控件

本文介绍如何在VS2005中将Menu控件绑定到DataSet,而非传统的XML或Website文件。通过将DataSet转换为XmlDataDocument,实现控件的数据绑定,并提供了一个递归填充TreeView控件的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VS2005中,menu和treeview控件是从xml和website文件读取的,但我想从dataset中读取,该如何做呢

 

查MSDN如下内容:
Menu   控件也可以绑定到数据。可以使用下面两种方法中的一种将   Menu   控件绑定到适当的数据源类型:

Menu   控件可以使用任意分层数据源控件,如   XmlDataSource   控件或   SiteMapDataSource   控件。若要绑定到分层数据源控件,请将   Menu   控件的   DataSourceID   属性设置为数据源控件的   ID   值。Menu   控件自动绑定到指定的数据源控件。这是绑定到数据的首选方法。

Menu   控件还可以绑定到   XmlDocument   对象。若要绑定到此数据源,请将   Menu   控件的   DataSource   属性设置为该数据源,然后调用   DataBind   方法。


所以可以得出如下结论:
menu控件既然可以绑定到   XmlDocument   对象,而我们已得到了dataset,那就通过用指定的   DataSet   初始化   XmlDataDocument   类的新实例,而XmlDataDocument这个类就是从XmlDocument继承而来的,也就是说menu控件可以绑定到XmlDataDocument对象,这样的话问题就解决了!

 

 

 

 

递归循环
参考:
private   void   BuildTree(TreeNode   parentNode)
        {
                SqlConnection   tempConnection   =   new   SqlConnection(m_ConnectionString);
                tempConnection.Open();
                try
                {
                        DataSet   tempSet   =   new   DataSet();
                        SqlDataAdapter   tempAdapter   =   new   SqlDataAdapter( "GetTree ",   tempConnection);
                        tempAdapter.SelectCommand.CommandType   =   CommandType.StoredProcedure;
                        tempAdapter.SelectCommand.Parameters.AddWithValue( "@parentId ",   int.Parse(parentNode.Value));
                        tempAdapter.Fill(tempSet);

                        foreach   (DataRow   row   in   tempSet.Tables[0].Rows)
                        {
                                TreeNode   tempNode   =   new   TreeNode();
                                tempNode.Text   =   row[ "name "].ToString();
                                tempNode.Value   =   row[ "id "].ToString();

                                parentNode.ChildNodes.Add(tempNode);

                                BuildTree(tempNode);
                        }

                }
                finally
                {
                        if   (tempConnection.State   ==   ConnectionState.Open)
                                tempConnection.Close();
                }      
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值