开发环境:WinXP SP3,VS2008,SQL2000
TreeView控件与SQL数据库的应用(遍历算法)
(数据与TreeView的绑定及Treeview的增加、删除、修改、遍历等数据库的操作)
这个是数据库连接及一些操作数据库的方法,文件:SqlManager.cs
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
namespace TreeViewText
{
class SqlManager
{
SqlConnection conn;
string strConn;
public SqlManager()
{
strConn = @" Data Source = .;Initial Catalog = TreeView; Integrated Security = SSPI; Persist Security Info = False; User ID = sa; Password = sa " ;
conn = new SqlConnection(strConn);
}
public DataSet GetDataSet()
{
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(querySqlCmd, conn);
try
{
conn.Open();
sda.Fill(ds);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, " 获取数据失败! " );
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
return ds;
}
public DataSet GetDataSet( string sqlCmd)
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlCmd, conn);
try
{
conn.Open();
da.Fill(ds);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, " 获取数据失败! " );
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
return ds;
}
public void RunSqlCommand( string sqlCmd)
{
SqlCommand cmd = new SqlCommand(sqlCmd, conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, " 操作数据库失败! " );
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}
private string m_querySqlCmd;
#region 获取SQL语句
public string querySqlCmd
{
get
{
if (m_querySqlCmd == null )
{
return " select * from TreeNode " ;
}
return querySqlCmd;
}
set
{
m_querySqlCmd = value;
}
}
#endregion
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
namespace TreeViewText
{
class SqlManager
{
SqlConnection conn;
string strConn;
public SqlManager()
{
strConn = @" Data Source = .;Initial Catalog = TreeView; Integrated Security = SSPI; Persist Security Info = False; User ID = sa; Password = sa " ;
conn = new SqlConnection(strConn);
}
public DataSet GetDataSet()
{
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(querySqlCmd, conn);
try
{
conn.Open();
sda.Fill(ds);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, " 获取数据失败! " );
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
return ds;
}
public DataSet GetDataSet( string sqlCmd)
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlCmd, conn);
try
{
conn.Open();
da.Fill(ds);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, " 获取数据失败! " );
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
return ds;
}
public void RunSqlCommand( string sqlCmd)
{
SqlCommand cmd = new SqlCommand(sqlCmd, conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, " 操作数据库失败! " );
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}
private string m_querySqlCmd;
#region 获取SQL语句
public string querySqlCmd
{
get
{
if (m_querySqlCmd == null )
{
return " select * from TreeNode " ;
}
return querySqlCmd;
}
set
{
m_querySqlCmd = value;
}
}
#endregion
}
}
主窗体:Form1.cs
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace TreeViewText
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load( object sender, EventArgs e)
{
// TreeNode treeNode1 = new TreeNode("节点1", new TreeNode[] { treeNode4, treeNode5 });
// treeView1.Nodes.AddRange(new TreeNode[] { treeNode1 });
}
#region TreeView节点编辑(没有修改数据库)
private void btnEdit_Click( object sender, EventArgs e)
{
if (treeView1.SelectedNode != null )
if ( ! treeView1.SelectedNode.IsEditing)
treeView1.SelectedNode.BeginEdit();
}
private void btnDelete_Click( object sender, EventArgs e)
{
if (treeView1.SelectedNode != null )
treeView1.SelectedNode.Remove();
}
private void btnAddNode_Click( object sender, EventArgs e)
{
TreeNode treeNodeTemp = new TreeNode(GetNodeText());
if (treeView1.Nodes.Count > 0 )
{
if (treeView1.SelectedNode != null )
{
if (treeView1.SelectedNode.Parent != null )
treeView1.SelectedNode.Parent.Nodes.Add(treeNodeTemp);
else
treeView1.Nodes.Add(treeNodeTemp);
}
}
else
{
treeView1.Nodes.Add(treeNodeTemp);
}
}
private void btnAddChild_Click( object sender, EventArgs e)
{
TreeNode treeNodeTemp = new TreeNode(GetNodeText());
if (treeView1.SelectedNode != null )
{
treeView1.SelectedNode.Nodes.Add(treeNodeTemp);
}
}
#region 获取文本框文本信息
/// <summary>
/// 获取文本框文本信息
/// </summary>
/// <returns> string </returns>
private string GetNodeText()
{
if ( ! String.IsNullOrEmpty(tbNodeName.Text.Trim()))
return tbNodeName.Text.Trim();
return " TreeNode " ;
}
#endregion
#endregion
/// <summary>
/// 查询数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnQuery_Click( object sender, EventArgs e)
{
SqlManager sqlManager = new SqlManager();
dataGridView1.DataSource = sqlManager.GetDataSet(sqlManager.querySqlCmd).Tables[ 0 ];
// 清空TreeView的内容
treeView1.Nodes.Clear();
// 递归函数,0为根节点的父节点ParentID的值
AddTreeView( 0 , (TreeNode) null );
// 非递归(两层分类显示)
// ShowTreeView((TreeNode)null);
// 展开所有树节点
// treeView1.Expand();
}
/// <summary>
/// 退出程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExit_Click( object sender, EventArgs e)
{
this .Close();
}
#region 获得数据库信息放入TreeView中 (递归)
private void AddTreeView( int ParentID, TreeNode parentNode)
{
SqlManager sqlManager = new SqlManager();
DataView dataTree = new DataView(sqlManager.GetDataSet().Tables[ 0 ]);
string filter = " ParentID = " + ParentID;
dataTree.RowFilter = filter;
foreach (DataRowView row in dataTree)
{
TreeNode node = new TreeNode();
// 处理根节点
if (parentNode == null )
{
node.Name = row[ " ID " ].ToString();
node.Text = row[ " Name " ].ToString();
treeView1.Nodes.Add(node);
AddTreeView(Int32.Parse(row[ " ID " ].ToString().Trim()), node);
}
// 处理子节点
else
{
node.Name = row[ " ID " ].ToString();
node.Text = row[ " Name " ].ToString();
parentNode.Nodes.Add(node);
AddTreeView(Int32.Parse(row[ " ID " ].ToString().Trim()), node);
}
}
}
#endregion
#region 获得数据库信息放入TreeView中 (非递归函数,根据Category来分类,仅支持两层分类)
/* private void ShowTreeView(TreeNode pNode)
{
treeView1.Nodes.Clear();
SqlManager sqlManager = new SqlManager();
DataView dataTree = new DataView(sqlManager.GetDataSet("select distinct Category from TreeNode").Tables[0]);
foreach (DataRowView row in dataTree)
{
TreeNode node = new TreeNode();
node.Text = row["Category"].ToString();
treeView1.Nodes.Add(node);
CreateChildNodes(node);
}
}
//获取第二层节点的的数据
private void CreateChildNodes(TreeNode treenode)
{
SqlManager sqlManager = new SqlManager();
DataView dataTree = new DataView(sqlManager.GetDataSet().Tables[0]);
dataTree.RowFilter = "Category = '" + treenode.Text + "'";
foreach (DataRowView row in dataTree)
{
TreeNode node = new TreeNode();
node.Text = row["Name"].ToString();
treenode.Nodes.Add(node);
}
} */
#endregion
/// <summary>
/// 添加记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAdd_Click( object sender, EventArgs e)
{
SqlManager sqlManager = new SqlManager();
string insertSqlCmd = String.Empty;
if (TextBoxValidator(tbParentID))
{
MessageBox.Show( " 请输入ParentID(父节点) " , " 提示 " );
tbParentID.Focus();
}
else if (TextBoxValidator(tbName))
{
MessageBox.Show( " 请输入Name(物品名称) " , " 提示 " );
tbName.Focus();
}
else if (TextBoxValidator(tbCategory))
{
MessageBox.Show( " 请输入Category(物品类别) " , " 提示 " );
tbCategory.Focus();
}
else
{
insertSqlCmd = " insert into TreeNode (ParentID,Name,Category) values ( " + tbParentID.Text.Trim() + " ,' " + tbName.Text.Trim() + " ',' " + tbCategory.Text.Trim() + " '); " ;
sqlManager.RunSqlCommand(insertSqlCmd);
dataGridView1.DataSource = sqlManager.GetDataSet(sqlManager.querySqlCmd).Tables[ 0 ];
treeView1.Nodes.Clear();
AddTreeView( 0 , (TreeNode) null );
// treeView1.ExpandAll();
}
}
/// <summary>
/// 修改记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnModify_Click( object sender, EventArgs e)
{
SqlManager sqlManager = new SqlManager();
string updateSqlCmd = String.Empty;
if (TextBoxValidator(tbID))
MessageBox.Show( " 请先选择一个产品 " , " 提示 " );
else if (TextBoxValidator(tbParentID))
{
MessageBox.Show( " 请输入ParentID(父节点) " , " 提示 " );
tbParentID.Focus();
}
else if (tbID.Text.Trim() == tbParentID.Text.Trim())
{
MessageBox.Show( " ID(产品ID)不能与ParentID(父节点)相同,请重新输入 " , " 提示 " );
tbParentID.Focus();
}
else if (TextBoxValidator(tbName))
{
MessageBox.Show( " 请输入Name(物品名称) " , " 提示 " );
tbName.Focus();
}
else if (TextBoxValidator(tbCategory))
{
MessageBox.Show( " 请输入Category(物品类别) " , " 提示 " );
tbCategory.Focus();
}
else
{
updateSqlCmd = " update TreeNode set ParentID = " + tbParentID.Text.Trim() + " ,Name = ' " + tbName.Text.Trim() + " ',Category = ' " + tbCategory.Text.Trim() + " ' where ID = " + tbID.Text.Trim() + " ; " ;
sqlManager.RunSqlCommand(updateSqlCmd);
dataGridView1.DataSource = sqlManager.GetDataSet(sqlManager.querySqlCmd).Tables[ 0 ];
treeView1.Nodes.Clear();
AddTreeView( 0 , (TreeNode) null );
// treeView1.ExpandAll();
}
}
/// <summary>
/// 删除记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRemove_Click( object sender, EventArgs e)
{
if (treeView1.SelectedNode != null )
{
TreeNode node = treeView1.SelectedNode;
// 删除所选节点及其所有子节点
DeleteTreeNode(node);
SqlManager sqlManager = new SqlManager();
dataGridView1.DataSource = sqlManager.GetDataSet(sqlManager.querySqlCmd).Tables[ 0 ];
}
}
/// <summary>
/// 删除所选节点及其所有子节点 (通过遍历节点删除对应的数据库数据)
/// </summary>
/// <param name="parentNode"></param>
private void DeleteTreeNode(TreeNode parentNode)
{
/* **************方法一:删除选中节点及其所有子节点************** */
int childCount = parentNode.Nodes.Count;
for ( int i = 0 ; i < childCount; i ++ )
{
TreeNode childNode = parentNode.Nodes[ 0 ];
if (childNode.Nodes.Count > 0 )
{
DeleteTreeNode(childNode);
}
else
{
DeleteNodeData(childNode);
childNode.Remove();
}
}
if (parentNode.Nodes.Count == 0 )
{
DeleteNodeData(parentNode);
parentNode.Remove();
}
/* **************方法二:删除选中节点及其所有子节点************** */
// TreeNodeCollection treeNodeCollection = parentNode.Nodes;
// int count = treeNodeCollection.Count;
// for (int i = 0; i < count; i++)
// {
// TreeNode node = treeNodeCollection[0];
// if (node.Nodes.Count > 0)
// {
// DeleteTreeNode(node);
// }
// else
// {
// DeleteNodeData(node);
// node.Remove();
// }
// }
// if (treeNodeCollection.Count == 0)
// {
// DeleteNodeData(parentNode);
// parentNode.Remove();
// }
}
/// <summary>
/// 方法三:删除选中节点的所有子节点(选中的主节点不会被删除)
/// </summary>
/// <param name="parentNode"></param>
private void DeleteTreeNodeWithoutPareNode(TreeNode parentNode)
{
int count = parentNode.Nodes.Count;
for ( int i = 0 ; i < count; i ++ )
{
TreeNode node = parentNode.Nodes[ 0 ];
DeleteTreeNodeWithoutPareNode(node);
DeleteNodeData(node);
node.Remove();
}
}
/// <summary>
/// 将要删除的节点传给该函数,利用节点的Name属性(ID)删除数据库数据
/// </summary>
/// <param name="node"></param>
private void DeleteNodeData(TreeNode node)
{
SqlManager sqlManager = new SqlManager();
string deleteSqlCmd = String.Empty;
deleteSqlCmd = " delete from TreeNode where ID = " + node.Name;
sqlManager.RunSqlCommand(deleteSqlCmd);
}
/// <summary>
/// 检查TextBox的Text属性是否为空或没有字符
/// </summary>
/// <param name="sender"></param>
/// <returns> true/false </returns>
private bool TextBoxValidator( object sender)
{
if (String.IsNullOrEmpty(((TextBox)sender).Text))
return true ;
else
return false ;
}
/// <summary>
/// TreeView中节点选择事件,用来将数据放入TextBox
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeView1_AfterSelect( object sender, TreeViewEventArgs e)
{
string treeNodeID = treeView1.SelectedNode.Name;
SqlManager sqlManager = new SqlManager();
DataTable dt;
dt = sqlManager.GetDataSet( " select * from TreeNode where ID = ' " + treeNodeID + " ' " ).Tables[ 0 ];
if (dt.Rows.Count > 0 )
{
tbID.Text = dt.Rows[ 0 ][ " ID " ].ToString();
tbParentID.Text = dt.Rows[ 0 ][ " ParentID " ].ToString();
tbName.Text = dt.Rows[ 0 ][ " Name " ].ToString();
tbCategory.Text = dt.Rows[ 0 ][ " Category " ].ToString();
}
}
private void dataGridView1_SelectionChanged( object sender, EventArgs e)
{
if (dataGridView1.SelectedCells.Count != 0 )
{
tbID.Text = dataGridView1.CurrentRow.Cells[ " ID " ].Value.ToString();
tbParentID.Text = dataGridView1.CurrentRow.Cells[ " ParentID " ].Value.ToString();
tbName.Text = dataGridView1.CurrentRow.Cells[ " Name " ].Value.ToString();
tbCategory.Text = dataGridView1.CurrentRow.Cells[ " Category " ].Value.ToString();
}
}
private void 展开下一层节点ToolStripMenuItem_Click( object sender, EventArgs e)
{
if (treeView1.SelectedNode != null )
{
treeView1.SelectedNode.Expand();
}
}
private void 展开所有节点ToolStripMenuItem_Click( object sender, EventArgs e)
{
if (treeView1.SelectedNode != null )
{
treeView1.SelectedNode.ExpandAll();
}
}
private void 选中节点的所有子节点ToolStripMenuItem_Click( object sender, EventArgs e)
{
if (treeView1.SelectedNode != null )
{
TreeNode node = treeView1.SelectedNode;
// 删除所选节点的所有子节点
DeleteTreeNodeWithoutPareNode(node);
SqlManager sqlManager = new SqlManager();
dataGridView1.DataSource = sqlManager.GetDataSet(sqlManager.querySqlCmd).Tables[ 0 ];
}
}
private void 节点及其所有子节点ToolStripMenuItem_Click( object sender, EventArgs e)
{
btnRemove_Click( null , null );
}
private void treeView1_MouseDown( object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
TreeNode treenode = treeView1.GetNodeAt(e.X, e.Y);
if (treenode != null && treenode.Bounds.Contains(e.X, e.Y))
{
treeView1.SelectedNode = treenode;
}
else
{
treeView1.SelectedNode = null ;
}
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace TreeViewText
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load( object sender, EventArgs e)
{
// TreeNode treeNode1 = new TreeNode("节点1", new TreeNode[] { treeNode4, treeNode5 });
// treeView1.Nodes.AddRange(new TreeNode[] { treeNode1 });
}
#region TreeView节点编辑(没有修改数据库)
private void btnEdit_Click( object sender, EventArgs e)
{
if (treeView1.SelectedNode != null )
if ( ! treeView1.SelectedNode.IsEditing)
treeView1.SelectedNode.BeginEdit();
}
private void btnDelete_Click( object sender, EventArgs e)
{
if (treeView1.SelectedNode != null )
treeView1.SelectedNode.Remove();
}
private void btnAddNode_Click( object sender, EventArgs e)
{
TreeNode treeNodeTemp = new TreeNode(GetNodeText());
if (treeView1.Nodes.Count > 0 )
{
if (treeView1.SelectedNode != null )
{
if (treeView1.SelectedNode.Parent != null )
treeView1.SelectedNode.Parent.Nodes.Add(treeNodeTemp);
else
treeView1.Nodes.Add(treeNodeTemp);
}
}
else
{
treeView1.Nodes.Add(treeNodeTemp);
}
}
private void btnAddChild_Click( object sender, EventArgs e)
{
TreeNode treeNodeTemp = new TreeNode(GetNodeText());
if (treeView1.SelectedNode != null )
{
treeView1.SelectedNode.Nodes.Add(treeNodeTemp);
}
}
#region 获取文本框文本信息
/// <summary>
/// 获取文本框文本信息
/// </summary>
/// <returns> string </returns>
private string GetNodeText()
{
if ( ! String.IsNullOrEmpty(tbNodeName.Text.Trim()))
return tbNodeName.Text.Trim();
return " TreeNode " ;
}
#endregion
#endregion
/// <summary>
/// 查询数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnQuery_Click( object sender, EventArgs e)
{
SqlManager sqlManager = new SqlManager();
dataGridView1.DataSource = sqlManager.GetDataSet(sqlManager.querySqlCmd).Tables[ 0 ];
// 清空TreeView的内容
treeView1.Nodes.Clear();
// 递归函数,0为根节点的父节点ParentID的值
AddTreeView( 0 , (TreeNode) null );
// 非递归(两层分类显示)
// ShowTreeView((TreeNode)null);
// 展开所有树节点
// treeView1.Expand();
}
/// <summary>
/// 退出程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExit_Click( object sender, EventArgs e)
{
this .Close();
}
#region 获得数据库信息放入TreeView中 (递归)
private void AddTreeView( int ParentID, TreeNode parentNode)
{
SqlManager sqlManager = new SqlManager();
DataView dataTree = new DataView(sqlManager.GetDataSet().Tables[ 0 ]);
string filter = " ParentID = " + ParentID;
dataTree.RowFilter = filter;
foreach (DataRowView row in dataTree)
{
TreeNode node = new TreeNode();
// 处理根节点
if (parentNode == null )
{
node.Name = row[ " ID " ].ToString();
node.Text = row[ " Name " ].ToString();
treeView1.Nodes.Add(node);
AddTreeView(Int32.Parse(row[ " ID " ].ToString().Trim()), node);
}
// 处理子节点
else
{
node.Name = row[ " ID " ].ToString();
node.Text = row[ " Name " ].ToString();
parentNode.Nodes.Add(node);
AddTreeView(Int32.Parse(row[ " ID " ].ToString().Trim()), node);
}
}
}
#endregion
#region 获得数据库信息放入TreeView中 (非递归函数,根据Category来分类,仅支持两层分类)
/* private void ShowTreeView(TreeNode pNode)
{
treeView1.Nodes.Clear();
SqlManager sqlManager = new SqlManager();
DataView dataTree = new DataView(sqlManager.GetDataSet("select distinct Category from TreeNode").Tables[0]);
foreach (DataRowView row in dataTree)
{
TreeNode node = new TreeNode();
node.Text = row["Category"].ToString();
treeView1.Nodes.Add(node);
CreateChildNodes(node);
}
}
//获取第二层节点的的数据
private void CreateChildNodes(TreeNode treenode)
{
SqlManager sqlManager = new SqlManager();
DataView dataTree = new DataView(sqlManager.GetDataSet().Tables[0]);
dataTree.RowFilter = "Category = '" + treenode.Text + "'";
foreach (DataRowView row in dataTree)
{
TreeNode node = new TreeNode();
node.Text = row["Name"].ToString();
treenode.Nodes.Add(node);
}
} */
#endregion
/// <summary>
/// 添加记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAdd_Click( object sender, EventArgs e)
{
SqlManager sqlManager = new SqlManager();
string insertSqlCmd = String.Empty;
if (TextBoxValidator(tbParentID))
{
MessageBox.Show( " 请输入ParentID(父节点) " , " 提示 " );
tbParentID.Focus();
}
else if (TextBoxValidator(tbName))
{
MessageBox.Show( " 请输入Name(物品名称) " , " 提示 " );
tbName.Focus();
}
else if (TextBoxValidator(tbCategory))
{
MessageBox.Show( " 请输入Category(物品类别) " , " 提示 " );
tbCategory.Focus();
}
else
{
insertSqlCmd = " insert into TreeNode (ParentID,Name,Category) values ( " + tbParentID.Text.Trim() + " ,' " + tbName.Text.Trim() + " ',' " + tbCategory.Text.Trim() + " '); " ;
sqlManager.RunSqlCommand(insertSqlCmd);
dataGridView1.DataSource = sqlManager.GetDataSet(sqlManager.querySqlCmd).Tables[ 0 ];
treeView1.Nodes.Clear();
AddTreeView( 0 , (TreeNode) null );
// treeView1.ExpandAll();
}
}
/// <summary>
/// 修改记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnModify_Click( object sender, EventArgs e)
{
SqlManager sqlManager = new SqlManager();
string updateSqlCmd = String.Empty;
if (TextBoxValidator(tbID))
MessageBox.Show( " 请先选择一个产品 " , " 提示 " );
else if (TextBoxValidator(tbParentID))
{
MessageBox.Show( " 请输入ParentID(父节点) " , " 提示 " );
tbParentID.Focus();
}
else if (tbID.Text.Trim() == tbParentID.Text.Trim())
{
MessageBox.Show( " ID(产品ID)不能与ParentID(父节点)相同,请重新输入 " , " 提示 " );
tbParentID.Focus();
}
else if (TextBoxValidator(tbName))
{
MessageBox.Show( " 请输入Name(物品名称) " , " 提示 " );
tbName.Focus();
}
else if (TextBoxValidator(tbCategory))
{
MessageBox.Show( " 请输入Category(物品类别) " , " 提示 " );
tbCategory.Focus();
}
else
{
updateSqlCmd = " update TreeNode set ParentID = " + tbParentID.Text.Trim() + " ,Name = ' " + tbName.Text.Trim() + " ',Category = ' " + tbCategory.Text.Trim() + " ' where ID = " + tbID.Text.Trim() + " ; " ;
sqlManager.RunSqlCommand(updateSqlCmd);
dataGridView1.DataSource = sqlManager.GetDataSet(sqlManager.querySqlCmd).Tables[ 0 ];
treeView1.Nodes.Clear();
AddTreeView( 0 , (TreeNode) null );
// treeView1.ExpandAll();
}
}
/// <summary>
/// 删除记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRemove_Click( object sender, EventArgs e)
{
if (treeView1.SelectedNode != null )
{
TreeNode node = treeView1.SelectedNode;
// 删除所选节点及其所有子节点
DeleteTreeNode(node);
SqlManager sqlManager = new SqlManager();
dataGridView1.DataSource = sqlManager.GetDataSet(sqlManager.querySqlCmd).Tables[ 0 ];
}
}
/// <summary>
/// 删除所选节点及其所有子节点 (通过遍历节点删除对应的数据库数据)
/// </summary>
/// <param name="parentNode"></param>
private void DeleteTreeNode(TreeNode parentNode)
{
/* **************方法一:删除选中节点及其所有子节点************** */
int childCount = parentNode.Nodes.Count;
for ( int i = 0 ; i < childCount; i ++ )
{
TreeNode childNode = parentNode.Nodes[ 0 ];
if (childNode.Nodes.Count > 0 )
{
DeleteTreeNode(childNode);
}
else
{
DeleteNodeData(childNode);
childNode.Remove();
}
}
if (parentNode.Nodes.Count == 0 )
{
DeleteNodeData(parentNode);
parentNode.Remove();
}
/* **************方法二:删除选中节点及其所有子节点************** */
// TreeNodeCollection treeNodeCollection = parentNode.Nodes;
// int count = treeNodeCollection.Count;
// for (int i = 0; i < count; i++)
// {
// TreeNode node = treeNodeCollection[0];
// if (node.Nodes.Count > 0)
// {
// DeleteTreeNode(node);
// }
// else
// {
// DeleteNodeData(node);
// node.Remove();
// }
// }
// if (treeNodeCollection.Count == 0)
// {
// DeleteNodeData(parentNode);
// parentNode.Remove();
// }
}
/// <summary>
/// 方法三:删除选中节点的所有子节点(选中的主节点不会被删除)
/// </summary>
/// <param name="parentNode"></param>
private void DeleteTreeNodeWithoutPareNode(TreeNode parentNode)
{
int count = parentNode.Nodes.Count;
for ( int i = 0 ; i < count; i ++ )
{
TreeNode node = parentNode.Nodes[ 0 ];
DeleteTreeNodeWithoutPareNode(node);
DeleteNodeData(node);
node.Remove();
}
}
/// <summary>
/// 将要删除的节点传给该函数,利用节点的Name属性(ID)删除数据库数据
/// </summary>
/// <param name="node"></param>
private void DeleteNodeData(TreeNode node)
{
SqlManager sqlManager = new SqlManager();
string deleteSqlCmd = String.Empty;
deleteSqlCmd = " delete from TreeNode where ID = " + node.Name;
sqlManager.RunSqlCommand(deleteSqlCmd);
}
/// <summary>
/// 检查TextBox的Text属性是否为空或没有字符
/// </summary>
/// <param name="sender"></param>
/// <returns> true/false </returns>
private bool TextBoxValidator( object sender)
{
if (String.IsNullOrEmpty(((TextBox)sender).Text))
return true ;
else
return false ;
}
/// <summary>
/// TreeView中节点选择事件,用来将数据放入TextBox
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeView1_AfterSelect( object sender, TreeViewEventArgs e)
{
string treeNodeID = treeView1.SelectedNode.Name;
SqlManager sqlManager = new SqlManager();
DataTable dt;
dt = sqlManager.GetDataSet( " select * from TreeNode where ID = ' " + treeNodeID + " ' " ).Tables[ 0 ];
if (dt.Rows.Count > 0 )
{
tbID.Text = dt.Rows[ 0 ][ " ID " ].ToString();
tbParentID.Text = dt.Rows[ 0 ][ " ParentID " ].ToString();
tbName.Text = dt.Rows[ 0 ][ " Name " ].ToString();
tbCategory.Text = dt.Rows[ 0 ][ " Category " ].ToString();
}
}
private void dataGridView1_SelectionChanged( object sender, EventArgs e)
{
if (dataGridView1.SelectedCells.Count != 0 )
{
tbID.Text = dataGridView1.CurrentRow.Cells[ " ID " ].Value.ToString();
tbParentID.Text = dataGridView1.CurrentRow.Cells[ " ParentID " ].Value.ToString();
tbName.Text = dataGridView1.CurrentRow.Cells[ " Name " ].Value.ToString();
tbCategory.Text = dataGridView1.CurrentRow.Cells[ " Category " ].Value.ToString();
}
}
private void 展开下一层节点ToolStripMenuItem_Click( object sender, EventArgs e)
{
if (treeView1.SelectedNode != null )
{
treeView1.SelectedNode.Expand();
}
}
private void 展开所有节点ToolStripMenuItem_Click( object sender, EventArgs e)
{
if (treeView1.SelectedNode != null )
{
treeView1.SelectedNode.ExpandAll();
}
}
private void 选中节点的所有子节点ToolStripMenuItem_Click( object sender, EventArgs e)
{
if (treeView1.SelectedNode != null )
{
TreeNode node = treeView1.SelectedNode;
// 删除所选节点的所有子节点
DeleteTreeNodeWithoutPareNode(node);
SqlManager sqlManager = new SqlManager();
dataGridView1.DataSource = sqlManager.GetDataSet(sqlManager.querySqlCmd).Tables[ 0 ];
}
}
private void 节点及其所有子节点ToolStripMenuItem_Click( object sender, EventArgs e)
{
btnRemove_Click( null , null );
}
private void treeView1_MouseDown( object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
TreeNode treenode = treeView1.GetNodeAt(e.X, e.Y);
if (treenode != null && treenode.Bounds.Contains(e.X, e.Y))
{
treeView1.SelectedNode = treenode;
}
else
{
treeView1.SelectedNode = null ;
}
}
}
}
}
Form1.Designer.cs文件:
namespace TreeViewText
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
this.treeView1 = new System.Windows.Forms.TreeView();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.展开树ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.展开所有节点ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.删除ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.节点ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.节点及其所有子节点ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.label1 = new System.Windows.Forms.Label();
this.btnDelete = new System.Windows.Forms.Button();
this.btnAddNode = new System.Windows.Forms.Button();
this.btnAddChild = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.btnQuery = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.btnEdit = new System.Windows.Forms.Button();
this.tbNodeName = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.btnModify = new System.Windows.Forms.Button();
this.btnRemove = new System.Windows.Forms.Button();
this.btnAdd = new System.Windows.Forms.Button();
this.tbID = new System.Windows.Forms.TextBox();
this.tbName = new System.Windows.Forms.TextBox();
this.tbCategory = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.tbParentID = new System.Windows.Forms.TextBox();
this.label7 = new System.Windows.Forms.Label();
this.panel1 = new System.Windows.Forms.Panel();
this.contextMenuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.ContextMenuStrip = this.contextMenuStrip1;
this.treeView1.HideSelection = false;
this.treeView1.LabelEdit = true;
this.treeView1.Location = new System.Drawing.Point(12, 24);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(208, 326);
this.treeView1.TabIndex = 0;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
this.treeView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseDown);
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.展开树ToolStripMenuItem,
this.展开所有节点ToolStripMenuItem,
this.删除ToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.ShowImageMargin = false;
this.contextMenuStrip1.Size = new System.Drawing.Size(130, 70);
//
// 展开树ToolStripMenuItem
//
this.展开树ToolStripMenuItem.Name = "展开树ToolStripMenuItem";
this.展开树ToolStripMenuItem.Size = new System.Drawing.Size(129, 22);
this.展开树ToolStripMenuItem.Text = "展开下一层节点";
this.展开树ToolStripMenuItem.Click += new System.EventHandler(this.展开下一层节点ToolStripMenuItem_Click);
//
// 展开所有节点ToolStripMenuItem
//
this.展开所有节点ToolStripMenuItem.Name = "展开所有节点ToolStripMenuItem";
this.展开所有节点ToolStripMenuItem.Size = new System.Drawing.Size(129, 22);
this.展开所有节点ToolStripMenuItem.Text = "展开所有子节点";
this.展开所有节点ToolStripMenuItem.Click += new System.EventHandler(this.展开所有节点ToolStripMenuItem_Click);
//
// 删除ToolStripMenuItem
//
this.删除ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.节点ToolStripMenuItem,
this.节点及其所有子节点ToolStripMenuItem});
this.删除ToolStripMenuItem.Name = "删除ToolStripMenuItem";
this.删除ToolStripMenuItem.Size = new System.Drawing.Size(129, 22);
this.删除ToolStripMenuItem.Text = "删除节点->";
//
// 节点ToolStripMenuItem
//
this.节点ToolStripMenuItem.Name = "节点ToolStripMenuItem";
this.节点ToolStripMenuItem.Size = new System.Drawing.Size(202, 22);
this.节点ToolStripMenuItem.Text = "选中节点的所有子节点";
this.节点ToolStripMenuItem.Click += new System.EventHandler(this.选中节点的所有子节点ToolStripMenuItem_Click);
//
// 节点及其所有子节点ToolStripMenuItem
//
this.节点及其所有子节点ToolStripMenuItem.Name = "节点及其所有子节点ToolStripMenuItem";
this.节点及其所有子节点ToolStripMenuItem.Size = new System.Drawing.Size(202, 22);
this.节点及其所有子节点ToolStripMenuItem.Text = "选中节点及其所有子节点";
this.节点及其所有子节点ToolStripMenuItem.Click += new System.EventHandler(this.节点及其所有子节点ToolStripMenuItem_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 1;
this.label1.Text = "节点:";
//
// btnDelete
//
this.btnDelete.Enabled = false;
this.btnDelete.Location = new System.Drawing.Point(113, 370);
this.btnDelete.Name = "btnDelete";
this.btnDelete.Size = new System.Drawing.Size(75, 23);
this.btnDelete.TabIndex = 16;
this.btnDelete.Text = "删除";
this.btnDelete.UseVisualStyleBackColor = true;
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// btnAddNode
//
this.btnAddNode.Location = new System.Drawing.Point(5, 30);
this.btnAddNode.Name = "btnAddNode";
this.btnAddNode.Size = new System.Drawing.Size(75, 23);
this.btnAddNode.TabIndex = 20;
this.btnAddNode.Text = "添加根";
this.btnAddNode.UseVisualStyleBackColor = true;
this.btnAddNode.Click += new System.EventHandler(this.btnAddNode_Click);
//
// btnAddChild
//
this.btnAddChild.Location = new System.Drawing.Point(99, 30);
this.btnAddChild.Name = "btnAddChild";
this.btnAddChild.Size = new System.Drawing.Size(75, 23);
this.btnAddChild.TabIndex = 21;
this.btnAddChild.Text = "添加子级";
this.btnAddChild.UseVisualStyleBackColor = true;
this.btnAddChild.Click += new System.EventHandler(this.btnAddChild_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(224, 9);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(65, 12);
this.label2.TabIndex = 7;
this.label2.Text = "节点信息:";
//
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dataGridView1.BackgroundColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
this.dataGridView1.DefaultCellStyle = dataGridViewCellStyle2;
this.dataGridView1.Location = new System.Drawing.Point(226, 24);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.ReadOnly = true;
dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle3.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dataGridView1.RowHeadersDefaultCellStyle = dataGridViewCellStyle3;
this.dataGridView1.RowTemplate.Height = 23;
this.dataGridView1.Size = new System.Drawing.Size(346, 326);
this.dataGridView1.TabIndex = 1;
this.dataGridView1.SelectionChanged += new System.EventHandler(this.dataGridView1_SelectionChanged);
//
// btnQuery
//
this.btnQuery.Location = new System.Drawing.Point(226, 370);
this.btnQuery.Name = "btnQuery";
this.btnQuery.Size = new System.Drawing.Size(75, 44);
this.btnQuery.TabIndex = 2;
this.btnQuery.Text = "查询(&S)";
this.btnQuery.UseVisualStyleBackColor = true;
this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(226, 429);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(75, 44);
this.btnExit.TabIndex = 3;
this.btnExit.Text = "退出(&C)";
this.btnExit.UseVisualStyleBackColor = true;
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// btnEdit
//
this.btnEdit.Enabled = false;
this.btnEdit.Location = new System.Drawing.Point(19, 370);
this.btnEdit.Name = "btnEdit";
this.btnEdit.Size = new System.Drawing.Size(75, 23);
this.btnEdit.TabIndex = 15;
this.btnEdit.Text = "编辑";
this.btnEdit.UseVisualStyleBackColor = true;
this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
//
// tbNodeName
//
this.tbNodeName.Location = new System.Drawing.Point(74, 3);
this.tbNodeName.Name = "tbNodeName";
this.tbNodeName.Size = new System.Drawing.Size(100, 21);
this.tbNodeName.TabIndex = 19;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(3, 6);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(65, 12);
this.label3.TabIndex = 18;
this.label3.Text = "节点名称:";
//
// btnModify
//
this.btnModify.Location = new System.Drawing.Point(409, 464);
this.btnModify.Name = "btnModify";
this.btnModify.Size = new System.Drawing.Size(75, 23);
this.btnModify.TabIndex = 13;
this.btnModify.Text = "修改(&M)";
this.btnModify.UseVisualStyleBackColor = true;
this.btnModify.Click += new System.EventHandler(this.btnModify_Click);
//
// btnRemove
//
this.btnRemove.Location = new System.Drawing.Point(490, 464);
this.btnRemove.Name = "btnRemove";
this.btnRemove.Size = new System.Drawing.Size(75, 23);
this.btnRemove.TabIndex = 14;
this.btnRemove.Text = "删除(&D)";
this.btnRemove.UseVisualStyleBackColor = true;
this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(328, 464);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(75, 23);
this.btnAdd.TabIndex = 12;
this.btnAdd.Text = "新增(&A)";
this.btnAdd.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// tbID
//
this.tbID.Location = new System.Drawing.Point(465, 356);
this.tbID.Name = "tbID";
this.tbID.ReadOnly = true;
this.tbID.Size = new System.Drawing.Size(100, 21);
this.tbID.TabIndex = 5;
//
// tbName
//
this.tbName.Location = new System.Drawing.Point(465, 410);
this.tbName.Name = "tbName";
this.tbName.Size = new System.Drawing.Size(100, 21);
this.tbName.TabIndex = 9;
//
// tbCategory
//
this.tbCategory.Location = new System.Drawing.Point(465, 437);
this.tbCategory.Name = "tbCategory";
this.tbCategory.Size = new System.Drawing.Size(100, 21);
this.tbCategory.TabIndex = 11;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(382, 359);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(77, 12);
this.label4.TabIndex = 4;
this.label4.Text = "ID(产品ID):";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(358, 413);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(101, 12);
this.label5.TabIndex = 8;
this.label5.Text = "Name(物品名称):";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(334, 440);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(125, 12);
this.label6.TabIndex = 10;
this.label6.Text = "Category(物品类别):";
//
// tbParentID
//
this.tbParentID.Location = new System.Drawing.Point(465, 383);
this.tbParentID.Name = "tbParentID";
this.tbParentID.Size = new System.Drawing.Size(100, 21);
this.tbParentID.TabIndex = 7;
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(346, 386);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(113, 12);
this.label7.TabIndex = 6;
this.label7.Text = "ParentID(父节点):";
//
// panel1
//
this.panel1.Controls.Add(this.label3);
this.panel1.Controls.Add(this.btnAddNode);
this.panel1.Controls.Add(this.btnAddChild);
this.panel1.Controls.Add(this.tbNodeName);
this.panel1.Enabled = false;
this.panel1.Location = new System.Drawing.Point(14, 410);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(177, 56);
this.panel1.TabIndex = 17;
//
// Form1
//
this.AcceptButton = this.btnQuery;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(577, 496);
this.Controls.Add(this.panel1);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label7);
this.Controls.Add(this.label4);
this.Controls.Add(this.tbCategory);
this.Controls.Add(this.tbName);
this.Controls.Add(this.tbParentID);
this.Controls.Add(this.tbID);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.btnRemove);
this.Controls.Add(this.btnModify);
this.Controls.Add(this.btnEdit);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnQuery);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnDelete);
this.Controls.Add(this.label1);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "TreeViewText by danch744";
this.Load += new System.EventHandler(this.Form1_Load);
this.contextMenuStrip1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnDelete;
private System.Windows.Forms.Button btnAddNode;
private System.Windows.Forms.Button btnAddChild;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button btnQuery;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.Button btnEdit;
private System.Windows.Forms.TextBox tbNodeName;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnModify;
private System.Windows.Forms.Button btnRemove;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.TextBox tbID;
private System.Windows.Forms.TextBox tbName;
private System.Windows.Forms.TextBox tbCategory;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox tbParentID;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem 展开树ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 节点ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 节点及其所有子节点ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 删除ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 展开所有节点ToolStripMenuItem;
}
}
点上面的那个图标显示代码。另外说一下,数据库的定义就在那张图片那样定义,主键是ID,ID和ParentID都是int,其余两个都是nvarchar,数据库名:TreeView,表为TreeNode,具体可以从代码看出来。
要源代码及数据库的朋友请留下邮箱,第一次在博客园发帖,还请各位高手指点迷津,虚心接受批评。。。