1. 請高手指點如下兩個問題:TreeView數據綁定和右鍵菜單
你好,補充的剛看到,第二個問題,其實如果你選中某個節點然後再點擊下拉菜單上的增加(表示你想在選中的節點下面添加子節點),那麼增加的節點就是(選中節點下面的)子節點盡管此時新增的節點沒有子節點但是相對於整個樹來說增加的節點是子節點,因為樹的結構不僅僅是兩級,如果想增加一個母節點,那麼在樹空白部分點擊增加,那麼此時增加的節點是母節點且是根節點以下代碼我更新過了,並且測試了,除了插入資料庫部分我沒做,其他都可以實現,希望你看下我的注釋部分,那部分注釋是描述在界面上操作完之後對資料庫的操作需,有問題可以再問!!注意的地方.
你把我貼給你的代碼中的資料庫換成你自己所需要的,表名也是,我用的實例表結構如下:
ID Name ParentID
其實你要實現的一效果用一張表就可以完成,添加一個父親欄位即可,這樣,寫出來的程序,不管有多少級都能動態的表示出來,用到遞歸演算法,詳情看代碼:
以下代碼是在winform程序中寫的
namespace TreeViewTest
{
public partial class Form1 : Form
{
TreeNode tnPublic;
string newText = "";
public Form1()
{
InitializeComponent();
BindTreeView();
}
/// <summary>
/// 綁定資料庫
/// </summary>
protected void BindTreeView()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=localhost;database=TuShuManage;uid=sa;pwd=sa";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * From Type where ParentID=0";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new System.Data.DataSet();
da.Fill(ds, "aa");
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TreeNode tr = new TreeNode();
tr.Text = ds.Tables[0].Rows[i]["Name"].ToString();
tr.Tag = int.Parse(ds.Tables[0].Rows[i]["ID"].ToString());
this.treeView1.Nodes.Add(tr);
getChildNodes(int.Parse(ds.Tables[0].Rows[i]["ID"].ToString()), tr, ref treeView1);
}
}
}
protected void getChildNodes(int parentID, TreeNode tr, ref TreeView tv)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=localhost;database=TuShuManage;uid=sa;pwd=sa";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * From Type where ParentID=" + parentID + "";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new System.Data.DataSet();
da.Fill(ds, "aa");
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TreeNode trr = new TreeNode();
trr.Text = ds.Tables[0].Rows[i]["Name"].ToString();
trr.Tag = int.Parse(ds.Tables[0].Rows[i]["ID"].ToString());
tr.Nodes.Add(trr);
getChildNodes(int.Parse(ds.Tables[0].Rows[i]["ID"].ToString()), trr, ref tv);
}
}
}
private void treeView1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
//判斷是否點擊了右鍵,如果點擊了右鍵則進行相應程序的處理
this.treeView1.ContextMenuStrip = this.contextMenuStrip1; //出現右鍵菜單
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
tnPublic = e.Node;
e.Node.ExpandAll();
//this.treeView1.SelectedNode = null;
}
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
//如何判斷是父級還是子級
if (tnPublic.Tag != null && tnPublic.Tag.ToString() != "")
{
if (tnPublic.Nodes.Count == 0 && int.Parse(tnPublic.Tag.ToString()) != 0)
{
//是子級
if (e.ClickedItem.Text == "添加")
{
TreeNode tn = new TreeNode();
tn.Text = "新建節點";
tnPublic.Nodes.Add(tn);
tnPublic.ExpandAll();
this.treeView1.LabelEdit = true;
tn.BeginEdit();
//此時節點的新名稱保存在newText變數中
//界面上添加完之後,需要在資料庫里插入一條記錄
//這邊寫插入資料庫的代碼
//資料庫提交完之後再執行BindTreeView();方法
}
if (e.ClickedItem.Text == "刪除")
{
int id = int.Parse(tnPublic.Tag.ToString());//保存選中節點的ID,以便用來刪除資料庫中的記錄
TreeNode tn = tnPublic.Parent;
tn.Nodes.Remove(tnPublic);
//在treeview中刪除了節點之後,並沒有刪除資料庫中的相應數據,那麼在刪除完了之後,還需要對資料庫里相應的數據進行刪除
//資料庫提交完之後再執行BindTreeView();方法
}
if (e.ClickedItem.Text == "修改")
{
this.treeView1.LabelEdit = true;
tnPublic.ExpandAll();
tnPublic.BeginEdit();
//此時節點的新名稱保存在newText變數中
//界面上修改完之後,需要對資料庫進行相應的更改
//資料庫里的數據插入完之後
//資料庫提交完之後再執行BindTreeView();方法
}
}
else
{
//是父級
if (e.ClickedItem.Text == "添加")
{
TreeNode tn = new TreeNode();
tn.Text = "新建節點";
tnPublic.Nodes.Add(tn);
tnPublic.ExpandAll();
this.treeView1.LabelEdit = true;
tn.BeginEdit();
//此時節點的新名稱保存在newText變數中
//界面上添加完之後,需要在資料庫里插入一條記錄
//這邊寫插入資料庫的代碼
//資料庫提交完之後再執行BindTreeView();方法
}
if (e.ClickedItem.Text == "刪除")
{
int id = int.Parse(tnPublic.Tag.ToString());//保存選中節點的ID,以便用來刪除資料庫中的記錄
TreeNode tn = tnPublic.Parent;
tn.Nodes.Remove(tnPublic);
//在treeview中刪除了節點之後,並沒有刪除資料庫中的相應數據,那麼在刪除完了之後,還需要對資料庫里相應的數據進行刪除
//資料庫提交完之後再執行BindTreeView();方法
}
if (e.ClickedItem.Text == "修改")
{
this.treeView1.LabelEdit = true;
tnPublic.ExpandAll();
tnPublic.BeginEdit();
//此時節點的新名稱保存在newText變數中
//界面上修改完之後,需要對資料庫進行相應的更改
//資料庫里的數據插入完之後
//資料庫提交完之後再執行BindTreeView();方法
}
}
tnPublic = new TreeNode();
}
else
{
tnPublic = new TreeNode();
tnPublic.Tag = "0";
tnPublic.Text = "新建節點";
this.treeView1.Nodes.Add(tnPublic);
this.treeView1.LabelEdit = true;
tnPublic.BeginEdit();
//此時節點的新名稱保存在newText變數中
//資料庫提交完之後再執行BindTreeView();方法
}
}
private void treeView1_MouseClick(object sender, MouseEventArgs e)
{
}
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
newText = e.Label;//這里是添加節點或者修改節點時候保存的節點的新值用來保存到資料庫里的
}
}
}