using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;namespace WindowsApplication1
{
/// <summary>
/// Form7 的摘要说明。
/// </summary>
public class Form7 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form7()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
static void Main()
{
Application.Run(new Form7());
}
private System.Data.DataSet ds;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TreeView treeView1;
private System.Data.DataView dvTree; private System.Windows.Forms.DataGrid dg;
//DataSet ds=new DataSet();
private void data_Load()
{ ds=new DataSet();
OleDbConnection CN = new OleDbConnection();
try
{ CN.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\gj.mdb";
CN.Open(); OleDbCommand sqlCmd= new OleDbCommand();
sqlCmd.Connection = CN;
sqlCmd.CommandText = "select * from b";
sqlCmd.CommandType = CommandType.Text ;
OleDbDataAdapter adp = new OleDbDataAdapter(sqlCmd);
adp.Fill(ds); }
catch (Exception ex)
{
throw (ex);
}
finally
{
CN.Close();
}
//调用递归函数,完成树形结构的生成
AddTree(0, (TreeNode)null);
} // 递归添加树的节点
public void AddTree(int ParentID,TreeNode pNode)
{
DataView dvTree = new DataView(ds.Tables[0]);
//过滤ParentID,得到当前的所有子节点
dvTree.RowFilter = "[PARENTID] = " + ParentID;
foreach(DataRowView Row in dvTree)
{
if(pNode == null)
{ //'̀添加根节点
TreeNode Node = treeView1.Nodes.Add(Row["ClassName"].ToString());
AddTree(Int32.Parse(Row["ID"].ToString()),Node); //再次递归
}
else
{ //添加当前节点的子节点
TreeNode Node = pNode.Nodes.Add(Row["ClassName"].ToString());
AddTree(Int32.Parse(Row["ID"].ToString()),Node);//再次递归
}
}
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dg = new System.Windows.Forms.DataGrid();
this.ds = new System.Data.DataSet();
this.button1 = new System.Windows.Forms.Button();
this.treeView1 = new System.Windows.Forms.TreeView();
this.dvTree = new System.Data.DataView();
((System.ComponentModel.ISupportInitialize)(this.dg)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.ds)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dvTree)).BeginInit();
this.SuspendLayout();
//
// dg
//
this.dg.DataMember = "";
this.dg.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dg.Location = new System.Drawing.Point(8, 8);
this.dg.Name = "dg";
this.dg.Size = new System.Drawing.Size(272, 104);
this.dg.TabIndex = 0;
//
// ds
//
this.ds.DataSetName = "NewDataSet";
this.ds.Locale = new System.Globalization.CultureInfo("zh-CN");
//
// button1
//
this.button1.Location = new System.Drawing.Point(192, 208);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(40, 24);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.ItemHeight = 20;
this.treeView1.Location = new System.Drawing.Point(8, 128);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(121, 128);
this.treeView1.TabIndex = 2;
//
// Form7
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.treeView1);
this.Controls.Add(this.button1);
this.Controls.Add(this.dg);
this.Name = "Form7";
this.Text = "Form7";
((System.ComponentModel.ISupportInitialize)(this.dg)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.ds)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dvTree)).EndInit();
this.ResumeLayout(false); }
#endregion private void button1_Click(object sender, System.EventArgs e)
{
//dg.DataSource=ds.Tables[1];
data_Load();
dg.DataSource=ds.Tables[0];
AddTree(0, (TreeNode)null); }
///
}
}
为什么treeview 没有数据显示呀??? 帮帮我好不啦 . (一样的绑定datagird有数据.treeview 没有数据) 哪里错了.
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;namespace WindowsApplication1
{
/// <summary>
/// Form7 的摘要说明。
/// </summary>
public class Form7 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form7()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
static void Main()
{
Application.Run(new Form7());
}
private System.Data.DataSet ds;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TreeView treeView1;
private System.Data.DataView dvTree; private System.Windows.Forms.DataGrid dg;
//DataSet ds=new DataSet();
private void data_Load()
{ ds=new DataSet();
OleDbConnection CN = new OleDbConnection();
try
{ CN.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\gj.mdb";
CN.Open(); OleDbCommand sqlCmd= new OleDbCommand();
sqlCmd.Connection = CN;
sqlCmd.CommandText = "select * from b";
sqlCmd.CommandType = CommandType.Text ;
OleDbDataAdapter adp = new OleDbDataAdapter(sqlCmd);
adp.Fill(ds); }
catch (Exception ex)
{
throw (ex);
}
finally
{
CN.Close();
}
//调用递归函数,完成树形结构的生成
AddTree(0, (TreeNode)null);
} // 递归添加树的节点
public void AddTree(int ParentID,TreeNode pNode)
{
DataView dvTree = new DataView(ds.Tables[0]);
//过滤ParentID,得到当前的所有子节点
dvTree.RowFilter = "[PARENTID] = " + ParentID;
foreach(DataRowView Row in dvTree)
{
if(pNode == null)
{ //'̀添加根节点
TreeNode Node = treeView1.Nodes.Add(Row["ClassName"].ToString());
AddTree(Int32.Parse(Row["ID"].ToString()),Node); //再次递归
}
else
{ //添加当前节点的子节点
TreeNode Node = pNode.Nodes.Add(Row["ClassName"].ToString());
AddTree(Int32.Parse(Row["ID"].ToString()),Node);//再次递归
}
}
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dg = new System.Windows.Forms.DataGrid();
this.ds = new System.Data.DataSet();
this.button1 = new System.Windows.Forms.Button();
this.treeView1 = new System.Windows.Forms.TreeView();
this.dvTree = new System.Data.DataView();
((System.ComponentModel.ISupportInitialize)(this.dg)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.ds)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dvTree)).BeginInit();
this.SuspendLayout();
//
// dg
//
this.dg.DataMember = "";
this.dg.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dg.Location = new System.Drawing.Point(8, 8);
this.dg.Name = "dg";
this.dg.Size = new System.Drawing.Size(272, 104);
this.dg.TabIndex = 0;
//
// ds
//
this.ds.DataSetName = "NewDataSet";
this.ds.Locale = new System.Globalization.CultureInfo("zh-CN");
//
// button1
//
this.button1.Location = new System.Drawing.Point(192, 208);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(40, 24);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.ItemHeight = 20;
this.treeView1.Location = new System.Drawing.Point(8, 128);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(121, 128);
this.treeView1.TabIndex = 2;
//
// Form7
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.treeView1);
this.Controls.Add(this.button1);
this.Controls.Add(this.dg);
this.Name = "Form7";
this.Text = "Form7";
((System.ComponentModel.ISupportInitialize)(this.dg)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.ds)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dvTree)).EndInit();
this.ResumeLayout(false); }
#endregion private void button1_Click(object sender, System.EventArgs e)
{
//dg.DataSource=ds.Tables[1];
data_Load();
dg.DataSource=ds.Tables[0];
AddTree(0, (TreeNode)null); }
///
}
}
为什么treeview 没有数据显示呀??? 帮帮我好不啦 . (一样的绑定datagird有数据.treeview 没有数据) 哪里错了.
http://blog.csdn.net/knight94/archive/2006/03/24/637699.aspx
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace WindowsApplication1
{
/// <summary>
/// Form8 的摘要说明。
/// </summary>
public class Form8 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Data.OleDb.OleDbConnection Connection1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button2;
private System.Data.DataSet dset;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form8()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
static void Main()
{
Application.Run(new Form8());
}
/// <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()
{
System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
this.treeView1 = new System.Windows.Forms.TreeView();
this.Connection1 = new System.Data.OleDb.OleDbConnection();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button2 = new System.Windows.Forms.Button();
this.dset = new System.Data.DataSet();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dset)).BeginInit();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.ImageIndex = ((int)(configurationAppSettings.GetValue("treeView1.ImageIndex", typeof(int))));
this.treeView1.ItemHeight = ((int)(configurationAppSettings.GetValue("treeView1.ItemHeight", typeof(int))));
this.treeView1.Location = new System.Drawing.Point(8, 8);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = ((int)(configurationAppSettings.GetValue("treeView1.SelectedImageIndex", typeof(int))));
this.treeView1.ShowLines = ((bool)(configurationAppSettings.GetValue("treeView1.ShowLines", typeof(bool))));
this.treeView1.Size = new System.Drawing.Size(112, 256);
this.treeView1.TabIndex = 0;
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(128, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(160, 192);
this.dataGrid1.TabIndex = 2;
//
// button2
//
this.button2.Location = new System.Drawing.Point(176, 232);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(104, 24);
this.button2.TabIndex = 3;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// dset
//
this.dset.DataSetName = "NewDataSet";
this.dset.Locale = new System.Globalization.CultureInfo("zh-CN");
//
// Form8
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button2);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.treeView1);
this.Name = "Form8";
this.Text = "Form8";
this.Load += new System.EventHandler(this.Form8_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dset)).EndInit();
this.ResumeLayout(false); }
#endregion private void Form8_Load(object sender, System.EventArgs e)
{
//InitialTree();
Connection1=new OleDbConnection(@"Provider=MicroSoft.Jet.OLEDB.4.0;Data Source=C:\GJ.mdb");
Connection1.Open();
OleDbCommand cmd=new OleDbCommand();
cmd.Connection=this.Connection1;
cmd.CommandText="select * from b"; OleDbDataAdapter ada=new OleDbDataAdapter(cmd.CommandText,Connection1);
DataSet set1=new DataSet();
ada.Fill(dset,"b");
dataGrid1.DataSource=dset;
}
// private string nodeName = "NodeName";//dataTable中节点名字段
// private string nodeText = "NodeName";//dataTable中用于显示节点标题的字段
// private DataTable dt;
//
// void InitialTree()
// {
// //这生成测试数据
// string[] s = new string[]{"0","1","2","3","00","01","02","03","04","05","030","031","032","033","034","0300","0301","0302","03000","03001","03002"};
//
// dt = new DataTable();
// dt.Columns.Add("NodeName", typeof(string));
//
// foreach(string n in s)
// {
// dt.Rows.Add(new object[]{n});
// }
//
// //楼主,这里设置你的数据源 dt = 你的datatable;
//
// foreach(DataRow dr in dt.Rows)
// {
// string path = dr[nodeName].ToString();
// string text = dr[nodeText].ToString();
// if (path.Length == 1)
// {
// TreeNode node = new TreeNode(text);
// node.Tag = path;
// this.treeView1.Nodes.Add(node);
// AddChildNode(node.Nodes, path);
// }
// }
// }
//
// void AddChildNode(TreeNodeCollection nodes, string strPNode)
// {
// foreach(DataRow dr in dt.Rows)
// {
// string path = dr[nodeName].ToString();
// string text = dr[nodeText].ToString();
// //找到子节点
// if ( (path.Length == strPNode.Length + 1) && path.StartsWith(strPNode))
// {
// TreeNode node = new TreeNode(text);
// node.Tag = path;
// nodes.Add(node);
// AddChildNode(node.Nodes, path);
// }
// }
// } private System.Windows.Forms.TreeNode GetNode(string nodeText,System.Windows.Forms.TreeNode node)
{
System.Windows.Forms.TreeNode ret=null;
if (node.Text==nodeText)
{
ret= node;
}
else
{
foreach(System.Windows.Forms.TreeNode subnode in node.Nodes)
{
ret= GetNode(nodeText,subnode);
}
}
return ret;
}
private DataView dv = null;
private void CreateTree() { dv = dset.Tables["b"].DefaultView; dv.Sort = "ParentID ASC"; DataRowView[] arrDRV = dv.FindRows( 0 );//Get root data info if( arrDRV.Length == 0 ) return; TreeNode tnNew = null; foreach( DataRowView drv in arrDRV ) { //tnNew = trvDBBinding.Nodes.Add( drv.Row["TypeName"].ToString() );
tnNew=treeView1.Nodes.Add( drv.Row["ClassName"].ToString() ); tnNew.Tag = drv.Row["ID"].ToString();//Save "TypeID" in node's tag CreateTreeNode( ref tnNew ); } } private void CreateTreeNode( ref TreeNode tnParent ) { DataRowView[] arrDRV = dv.FindRows( tnParent.Tag );//Get children data info if( arrDRV.Length == 0 ) return; TreeNode tnNew = null; foreach( DataRowView drv in arrDRV ) { tnNew = tnParent.Nodes.Add( drv.Row["ClassName"].ToString() ); tnNew.Tag = drv.Row["ID"].ToString();//Save "TypeID" in node's tag CreateTreeNode( ref tnNew ); } }
private void button2_Click(object sender, System.EventArgs e)
{
CreateTree();
}// }
}
datagird 有东西显示,就是 treeview没任何东西,空白一片,<img src="http://bbs.hidotnet.com/PostAttachment.aspx?PostID=23496&AttachmentID=795&guid=38c35f15-b785-433f-a091-e27f9e5a3c36 </a>
如图的功能而已,要是我的数据哭不对的的,帮我设计下,我什么都可以改,只要能实现功能,我和我的朋友几乎都是web form , 这是我第一个winform 程序,请大家多多关照点.