C#之Winform中treeview控件綁定數據庫


private DataSet ds;
private SqlDataAdapter sqlDataAdapter1;
private int maxnodeid;

private void Form1_Load(object sender, System.EventArgs e)
{
string strconn=ConfigurationSettings.AppSettings["ConnStr"];
sqlConnection1 = new SqlConnection(strconn);
this.sqlConnection1.Open();
//填充DataSet
this.CreateDataSet();
//從數據庫中讀取數據,通過遞歸生成樹。
InitTree(this.treeView1.Nodes,"0");

}


private void CreateDataSet()
{
this.sqlDataAdapter1=new SqlDataAdapter("select * from s_menu ",this.sqlConnection1);
this.ds=new DataSet();
this.sqlDataAdapter1.Fill(ds,"tree");
}
private void InitTree(TreeNodeCollection Nds,string parentId)
{
DataView dv=new DataView();
TreeNode tmpNd;
string intId;
dv.Table=ds.Tables["tree"];
dv.RowFilter="ParentId='" + parentId + "'" ;
foreach(DataRowView drv in dv)
{
tmpNd=new TreeNode();
tmpNd.Tag=drv["NodeId"].ToString();
tmpNd.Text=drv["NodeName"].ToString();
Nds.Add(tmpNd);
intId=drv["ParentId"].ToString();
InitTree(tmpNd.Nodes,tmpNd.Tag.ToString());
}
}
//新增節點操作
private void insert(string type)
{//判斷是新增樹節點,還是子節點.
string strinsert="insert into s_menu values('{0}','{1}','{2}')";
string strformat="";
if(type=="sub")
strformat=string.Format(strinsert,maxnodeid.ToString(),this.selectnode.Tag.ToString(),this.strcomm);
else
strformat=string.Format(strinsert,maxnodeid.ToString(),"0",this.strcomm);
SqlCommand cmd=new SqlCommand(strformat,this.sqlConnection1);
cmd.ExecuteNonQuery();
}
//為新增節點算出最大的節點值,並以此值作為新增的節點ID值
private int GetMaxNodeid()
{
int pre=0,last=0;
DataSet maxds=new DataSet();
this.sqlDataAdapter1=new SqlDataAdapter("select nodeid from s_menu order by nodeid",this.sqlConnection1);
this.sqlDataAdapter1.Fill(maxds);
for(int i=0;i{
if(i+1{
pre=int.Parse(maxds.Tables[0].Rows[i][0].ToString());
last=int.Parse(maxds.Tables[0].Rows[i+1][0].ToString());
if(last-pre!=1)
return pre+1;
}
}
return last+1;
}
private void getallnode(TreeNode tn)
{
foreach(TreeNode node in tn.Nodes)
{
list.Add(node.Tag.ToString());
if(node.Nodes.Count>0)
{
getallnode(node);
}
}
}

private void treeView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
//判斷是否點擊了某個節點
this.selectnode= this.treeView1.GetNodeAt (e.X ,e.Y );
if(selectnode==null)
this.isselected=false;
else
this.isselected=true;
}
private void menuAdd_Click(object sender, System.EventArgs e)
{//判斷是否點擊了某個節點,若沒有點擊了,則是新增一個樹節點
if(isselected==false)
{//算出新增樹節點的ID值
maxnodeid=GetMaxNodeid();
TreeNode tmpNd=new TreeNode();
//賦值
tmpNd.Tag=this.maxnodeid.ToString();
FormCommon frmCommon=new FormCommon();
DialogResult result= frmCommon.ShowDialog();
if(result==DialogResult.OK)
{//取到新增樹節點的文本值
tmpNd.Text=frmCommon.strcomm;
this.strcomm=frmCommon.strcomm;
//新增樹節點
this.treeView1.Nodes.Add(tmpNd);
//插入數據庫(說明插入的是樹節點)
this.insert("root");
//展開
this.selectnode.Expand();
}
}
else
{//判斷是否點擊了某個節點,若點擊了,則是新增一個子節點
this.contextAddSub();
}
}
private void contextAddSub()
{//得到新增子節點的ID值
maxnodeid=GetMaxNodeid();
TreeNode tmpNd=new TreeNode();
//賦值
tmpNd.Tag=this.maxnodeid.ToString();
FormCommon frmCommon=new FormCommon();
DialogResult result= frmCommon.ShowDialog();
if(result==DialogResult.OK)
{//取到新增樹節點的文本值
tmpNd.Text=frmCommon.strcomm;
this.strcomm=frmCommon.strcomm;
//新增子節點
this.selectnode.Nodes.Add(tmpNd);
//插入數據庫(說明插入的是子節點)
this.insert("sub");
//展開
this.treeView1.SelectedNode.Expand();
}
}
//刪除節點操作
private void menuDel_Click(object sender, System.EventArgs e)
{//新建一個ArrayList,用於保存要刪除的節點下邊的所有子節點
list=new ArrayList();
if(this.isselected==true)
{//得到刪除的節點下邊的所有子節點
getallnode(this.selectnode);
//把要刪除的節點也加進去
list.Add(this.selectnode.Tag.ToString());
//循環從數據庫中刪除
for(int i=0;i{
string strdel="delete s_menu where nodeid='{0}'";
string strformat="";
strformat=string.Format(strdel,list[i]);
SqlCommand cmd=new SqlCommand(strformat,this.sqlConnection1);
cmd.ExecuteNonQuery();
}
//從樹中刪除
this.selectnode.Remove();
}
}
//修改節點的值
private void menuEdit_Click(object sender, System.EventArgs e)
{
if(this.isselected==true)
{
FormCommon frmCommon=new FormCommon();
DialogResult result= frmCommon.ShowDialog();
if(result==DialogResult.OK)
{
string strdel="update s_menu set nodename= '{1}' where nodeid='{0}'";
string strformat="";
strformat=string.Format(strdel,this.selectnode.Tag.ToString(),frmCommon.strcomm);
SqlCommand cmd=new SqlCommand(strformat,this.sqlConnection1);
cmd.ExecuteNonQuery();
this.selectnode.Text=frmCommon.strcomm;
}
}
}
//遍歷所有節點.查找值
private void getvaluenode(TreeNodeCollection tn,string value)
{
foreach(TreeNode node in tn)
{
if(node.Nodes.Count>0)
{
getvaluenode(node.Nodes,value);
}
if(node.Text==value)
listnode.Add(node);
}
}
private void menuSearch_Click(object sender, System.EventArgs e)
{
int j,k;
this.listnode=new ArrayList();
FormCommon frmCommon=new FormCommon();
DialogResult result= frmCommon.ShowDialog();
if(result==DialogResult.OK)
{
TreeNode n =new TreeNode();
TreeNode temp=new TreeNode();
//下面的函數是填充listnode;
getvaluenode(this.treeView1.Nodes,frmCommon.strcomm);
for(int i=0;i{
j=0;k=0;
n=(TreeNode)listnode[i];
if (n != null)
{
temp=n;
//得到上面結點的數量,並將數量保存到變量j;
for(;n.Parent!=null;)
{
n=n.Parent;
j++;
}
//恢復原值
n=temp;
//新建一個樹結點數組做保存得到查詢到的所有節點.
TreeNode[] m=new TreeNode[j];
for(;n.Parent!=null;)
{
n=n.Parent;
m[k]=n;
k++;
}
for(int p=0;pm[p].Expand();
n=temp;
n.ForeColor=Color.Red;
}
}
}
}
private void treeView1_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
{
if(this.treeView1.SelectedNode.Text!=null)
{
string strdel="update s_menu set nodename= '{1}' where nodeid='{0}'";
string strformat="";strformat=string.Format(strdel,this.treeView1.SelectedNode.Tag.ToString(),e.Label.ToString());SqlCommand cmd=new SqlCommand(strformat,this.sqlConnection1);
cmd.ExecuteNonQuery();

}
}
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
this.listBox1.Items.Clear();
this.listBox1.Items.Add(this.treeView1.SelectedNode.FullPath.ToString());
}
}
} 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM