| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2217 人关注过本帖
标题:如何用treeview显示SQL里表的数据
只看楼主 加入收藏
黄兄
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-10-26
收藏
 问题点数:0 回复次数:7 
如何用treeview显示SQL里表的数据

如题。请赐教。

搜索更多相关主题的帖子: SQL 里表 treeview 数据 
2006-12-10 17:59
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
不就是动态加上去咯。

你想怎样排版的问题,难到你还想二维表啊
2006-12-10 19:14
黄兄
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-10-26
收藏
得分:0 

能贴出代码吗?不是很明白

2006-12-10 20:29
hxhfg
Rank: 1
等 级:新手上路
帖 子:284
专家分:0
注 册:2006-4-18
收藏
得分:0 
我也需要呀,那位好心能给一份代码参考一下吗?

2007-08-17 03:12
remex1980
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-4-16
收藏
得分:0 

LZ指的是有parent字段?

如果是这样的情况,先读出第一层的,绑定到TreeNodes上,然后每点击一个节点的时候,读取其子记录,再绑定:

.NET中的高级树控件
2007-08-17 17:20
guoxhvip
Rank: 8Rank: 8
来 自:聖西羅南看臺
等 级:贵宾
威 望:44
帖 子:4052
专家分:135
注 册:2006-10-8
收藏
得分:0 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace TreeViewSchool
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView tvwSchool;
private System.Windows.Forms.ListView lvwSchool;
private SqlConnection cnn = null;
private SqlCommand cmm = null;
private SqlDataAdapter adp = null;
private DataSet ds = null;
private DataSet dsxh = null;
private ListViewItem lstItem = null;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.Panel panel2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.tvwSchool = new System.Windows.Forms.TreeView();
this.lvwSchool = new System.Windows.Forms.ListView();
this.panel1 = new System.Windows.Forms.Panel();
this.splitter1 = new System.Windows.Forms.Splitter();
this.panel2 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// tvwSchool
//
this.tvwSchool.Dock = System.Windows.Forms.DockStyle.Fill;
this.tvwSchool.ImageIndex = -1;
this.tvwSchool.Location = new System.Drawing.Point(0, 0);
this.tvwSchool.Name = "tvwSchool";
this.tvwSchool.SelectedImageIndex = -1;
this.tvwSchool.Size = new System.Drawing.Size(128, 272);
this.tvwSchool.TabIndex = 0;
this.tvwSchool.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tvwSchool_AfterSelect);
//
// lvwSchool
//
this.lvwSchool.Dock = System.Windows.Forms.DockStyle.Fill;
this.lvwSchool.Location = new System.Drawing.Point(0, 0);
this.lvwSchool.Name = "lvwSchool";
this.lvwSchool.Size = new System.Drawing.Size(301, 272);
this.lvwSchool.TabIndex = 1;
//
// panel1
//
this.panel1.Controls.Add(this.tvwSchool);
this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(128, 272);
this.panel1.TabIndex = 3;
//
// splitter1
//
this.splitter1.Location = new System.Drawing.Point(128, 0);
this.splitter1.MinExtra = 0;
this.splitter1.MinSize = 0;
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(3, 272);
this.splitter1.TabIndex = 4;
this.splitter1.TabStop = false;
//
// panel2
//
this.panel2.Controls.Add(this.lvwSchool);
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel2.Location = new System.Drawing.Point(131, 0);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(301, 272);
this.panel2.TabIndex = 5;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(432, 272);
this.Controls.Add(this.panel2);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "school";
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
this.lvwSchool.Columns.Add("学号",80,HorizontalAlignment.Center);
this.lvwSchool.Columns.Add("姓名",100,HorizontalAlignment.Center);
this.lvwSchool.Columns.Add("成绩",105,HorizontalAlignment.Center);
this.lvwSchool.View = View.Details;

cnn = new SqlConnection("server = .; uid = sa; pwd = sqlpass; database = school");
adp = new SqlDataAdapter();

cmm = new SqlCommand("select xh from Class1",cnn);
dsxh = new DataSet("school");
adp.SelectCommand = cmm;
adp.Fill(dsxh,"Class1");

TreeNode node1 = new TreeNode(dsxh.DataSetName);
this.tvwSchool.Nodes.Add(node1);

node1 = new TreeNode(dsxh.Tables["Class1"].TableName);
this.tvwSchool.Nodes[0].Nodes.Add(node1);
foreach(DataRow r in dsxh.Tables["Class1"].Rows)
{
node1 = new TreeNode(r["xh"].ToString());
this.tvwSchool.Nodes[0].Nodes[0].Nodes.Add(node1);
}

cmm = new SqlCommand("select xh from Class2",cnn);
adp.SelectCommand = cmm;
adp.Fill(dsxh,"Class2");
TreeNode node2 = new TreeNode(dsxh.Tables["Class2"].TableName);
this.tvwSchool.Nodes[0].Nodes.Add(node2);
foreach(DataRow r in dsxh.Tables["Class2"].Rows)
{
node2 = new TreeNode(r["xh"].ToString());
this.tvwSchool.Nodes[0].Nodes[1].Nodes.Add(node2);
}
}

private void tvwSchool_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
this.lvwSchool.Items.Clear();
if(tvwSchool.SelectedNode.Parent != null && tvwSchool.SelectedNode.Nodes.Count != 0)
{
string bj = tvwSchool.SelectedNode.Text;
cmm = new SqlCommand("select * from " + bj + "",cnn);
ds = new DataSet("school");
adp.SelectCommand = cmm;
adp.Fill(ds,"Class");
for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
lstItem = new ListViewItem(ds.Tables[0].Rows[i][0].ToString());
for(int j = 0; j < ds.Tables[0].Columns.Count - 1; j++)
{
lstItem.SubItems.Add(ds.Tables[0].Rows[i][j + 1].ToString());
}
lvwSchool.Items.Add(lstItem);
}
}
else if(tvwSchool.SelectedNode.Parent != null && tvwSchool.SelectedNode.Nodes.Count == 0)
{
string xh = tvwSchool.SelectedNode.Text;
string bj = tvwSchool.SelectedNode.Parent.Text;
cmm = new SqlCommand("select * from " + bj +" where xh = '" + xh +"'",cnn);
adp.SelectCommand = cmm;
ds = new DataSet("school");
adp.Fill(ds,"Class");
for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
lstItem = new ListViewItem(ds.Tables[0].Rows[i][0].ToString());
for(int j = 0; j < ds.Tables[0].Columns.Count - 1; j++)
{
lstItem.SubItems.Add(ds.Tables[0].Rows[i][j + 1].ToString());
}
lvwSchool.Items.Add(lstItem);
}
}
}
}
}
数据库school有两个表Class1,Class2,代表两个班,结构一模一样(这只是做做练习所以数据库表没有范式化,如果做项目可不能这样建表)下面是数据库表的结构:

图片附件: 游客没有浏览图片的权限,请 登录注册

[此贴子已经被作者于2007-8-18 18:41:15编辑过]


愛生活 && 愛編程
2007-08-18 18:34
闫方举
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-7-23
收藏
得分:0 
我是初学者,也许对我有帮助
2010-09-23 20:49
fewelee
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-9-25
收藏
得分:0 
我是初学者,谢谢6楼,请多指教
2010-09-25 11:38
快速回复:如何用treeview显示SQL里表的数据
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014341 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved