关闭

asp.net TreeView控件与数据库连接

标签: asp.net数据库treedatasetsecurityobject
388人阅读 评论(0) 收藏 举报
分类:

http://blog.sina.com.cn/s/blog_634a305f0100mry2.html

首先添加TreeView控件

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    public void BindDataBase()
    {
        SqlConnection sqlCon = new SqlConnection();
        sqlCon.ConnectionString = "Data Source=PC-201010301842;Initial Catalog=Student;Integrated Security=True";
        SqlDataAdapter da=new SqlDataAdapter("select 学号,姓名 from info",sqlCon);
        DataSet ds = new DataSet();
        da.Fill(ds,"info");
        TreeNode tree1=new TreeNode("学生信息");
        this.TreeView1.Nodes.Add(tree1);
        for(int i=0;i<ds.Tables["info"].Rows.Count;i++)
        {
            TreeNode tree2=new TreeNode(ds.Tables["info"].Rows[i][1].ToString());
            tree1.ChildNodes.Add(tree2);
            for(int j=0;j<ds.Tables["info"].Columns.Count;j++)
            {
               TreeNode tree3=new TreeNode(ds.Tables["info"].Rows[i][j].ToString());
                tree2.ChildNodes.Add(tree3);
            }
        }

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDataBase();
            TreeView1.ShowLines = true;
            TreeView1.ExpandDepth = 1;
        }
    }
}

运行效果图如下:

asp.net <wbr>TreeView控件与数据库连接


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:72958次
    • 积分:891
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:81篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论