如题。请赐教。
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编辑过]