久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

C# TreeView 控件的綜合使用方法

 羊玉wngbx 2019-09-26

1,、概述

    該篇文章開發(fā)使用的語言c#,,環(huán)境visualstudio2010,sql數(shù)據(jù)庫.主要內容包括:

(1)treeView控件添加根節(jié)點,、子節(jié)點的基本方法,,節(jié)點的刪除。

(2)把treeView控件的節(jié)點數(shù)據(jù)保存到SQL數(shù)據(jù)包括中,,把數(shù)據(jù)庫數(shù)據(jù)表中的數(shù)據(jù)動態(tài)加載到treeView控件中,,控件節(jié)點的遞歸刪除(指的是遞歸刪除數(shù)據(jù)表的數(shù)據(jù))

2、TreeView控件的基本用法

     為了演示相關方法、屬性的用法,,通過vs2010創(chuàng)建一個winform項目,,在項目中添加一個窗體,其布局如圖2-1所示

 

 

圖2-1

其中TreeView控件的名稱為:treeview1,文本框的名稱為:txtNodeName,,“添加根節(jié)點”按鈕的名稱為:btnAddRootNode,,“添加子節(jié)點”按鈕的名稱為:btnAddSonNode,“刪除選中節(jié)點”按鈕的名稱:btnDelete

添加節(jié)點,、刪除節(jié)點的代碼如下所示:

2.1添加根節(jié)點

復制代碼

 1 private void btnAddRootNode_Click(object sender, EventArgs e)
 2   {
 3       //要添加的節(jié)點名稱為空,,即文本框是否為空
 4         if(string.IsNullOrEmpty(txtNodeName.Text.Trim()))
 5          {
 6              MessageBox.Show("要添加的節(jié)點名稱不能為空!");
 7              return;
 8          }
 9             //添加根節(jié)點
10             treeView1.Nodes.Add(txtNodeName.Text.Trim());
11            txtNodeName.Text = "";
12   }

復制代碼

2.2添加子節(jié)點

復制代碼

 1 private void btnAddSonNode_Click(object sender, EventArgs e)
 2    {
 3        //要添加的節(jié)點名稱為空,,即文本框是否為空
 4         if (string.IsNullOrEmpty(txtNodeName.Text.Trim()))
 5          {
 6              MessageBox.Show("要添加的節(jié)點名稱不能為空,!");
 7               return;
 8          }
 9          if(treeView1.SelectedNode==null)
10           {
11              MessageBox.Show("請選擇要添加子節(jié)點的節(jié)點!");
12               return;
13           }               treeView1.SelectedNode.Nodes.Add(txtNodeName.Text.Trim());
14     txtNodeName.Text = "";
15    }

復制代碼

2.3刪除選中節(jié)點

復制代碼

1  private void btnDelete_Click(object sender, EventArgs e)
2   {
3       if (treeView1.SelectedNode == null)
4          {
5               MessageBox.Show("請選擇要刪除的節(jié)點,!");
6               return;
7          }
8             treeView1.SelectedNode.Remove();
9   }

復制代碼

3,、TreeView控件在樹形菜單中的應用

    Treeview在產(chǎn)品類別管理、部門管理等樹狀多級菜單中的應用,,主要包括把樹控件中的數(shù)據(jù)保存到SQL數(shù)據(jù)表中,,把數(shù)據(jù)表中的數(shù)據(jù)動態(tài)加載到treeview控件中,以及刪除控件節(jié)點及數(shù)據(jù)表中的數(shù)據(jù),。

3.1數(shù)據(jù)表

create table TreeTest
(
 id int identity(1,1) primary key not null,//節(jié)點id
 nodeName nvarchar(50) not null,//節(jié)點名稱
 parentId int not null//節(jié)點父id
)

3.2把節(jié)點數(shù)據(jù)存儲到數(shù)據(jù)表

    把treeView添加的節(jié)點信息存儲到數(shù)據(jù)表,,包括根節(jié)點、子節(jié)點,,所用的窗體界面如圖2-1所示,,控件的名稱保持不變,只是執(zhí)行代碼變了,。

復制代碼

 1  private void btnAddRootNode_Click (object sender, EventArgs e)
 2   {
 3         if(string.IsNullOrEmpty(txtNodeName.Text.Trim()))
 4          {
 5              MessageBox.Show("請?zhí)顚懸砑拥墓?jié)點名稱,!");
 6               return;
 7          }
 8   string sql = "insert into TreeTest(nodeName,parentId) output inserted.id values"+"("+" "+"'"+ txtNodeName.Text.Trim()+"'"+","+"'"+0+"'"+")";
 9             int id = (int)sqlHelper.ExecuteScalar(sql);
10             TreeNode node1 = new TreeNode();
11             node1.Tag = id;//把自己的id存放在該節(jié)點tag對象里
12             node1.Text = txtNodeName.Text.Trim();
13             treeView1.Nodes.Add(node1);
14             txtNodeName.Text = "";
15         }

復制代碼

復制代碼

 1 private void btnAddSonNode_Click (object sender, EventArgs e)
 2    {
 3             int id;
 4             if(string.IsNullOrEmpty(txtNodeName.Text.Trim()))
 5             {
 6                 return;
 7             }
 8             if(treeView1.SelectedNode==null)
 9             {
10                 MessageBox.Show("請選擇父節(jié)點");
11                 return;
12             }
13             id =(int)treeView1.SelectedNode.Tag;//獲取父id
14             string sql = "insert into TreeTest(nodeName,parentId) output inserted.id values"+"(" + " " + "'" + txtNodeName.Text.Trim() + "'" + "," + "'" + id + "'" + ")";
15             int id1 = (int)sqlHelper.ExecuteScalar(sql);
16             TreeNode node1 = new TreeNode();
17             node1.Tag = id1;
18             node1.Text = txtNodeName.Text.Trim();
19             treeView1.SelectedNode.Nodes.Add(node1);
20             txtNodeName.Text = "";
21  }

復制代碼

3.3動態(tài)加載數(shù)據(jù)

    把數(shù)據(jù)表中的數(shù)據(jù)動態(tài)加載到treeView控件中,在treeView所在窗體的load事件中加載,,相應的代碼如下所示

復制代碼

 1 private void Form3_Load(object sender, EventArgs e)
 2  {      
 3      //加載數(shù)據(jù),,把數(shù)據(jù)加載到控件treeview1中   
 4      setTreeView(treeView1, 0);           
 5  }
 6 
 7  //調用的時候parentId以0值開始 setTreeView(treeView1, 0);
 8   private void setTreeView(TreeView tr1,int parentId)
 9  {
10   string sql = "select * from TreeTest where parentId=" + parentId;
11       DataTable ds= sqlHelper.ExecuteDataTable(sql);
12        if (ds.Rows.Count > 0)
13         {
14                int pId = -1;
15              foreach (DataRow row in ds.Rows)
16                {
17                     TreeNode node = new TreeNode();
18                     node.Text = row["nodeName"].ToString();
19                     node.Tag = (int)row["id"];
20                     pId = (int)row["parentId"];
21                     if (pId == 0)
22                     {
23                         //添加根節(jié)點
24                         tr1.Nodes.Add(node);
25                     }
26                     else
27                     {
28                         //添加根節(jié)點之外的其他節(jié)點
29                         RefreshChildNode(tr1,node,pId);
30                     }
31                     //查找以node為父節(jié)點的子節(jié)點
32                     setTreeView(tr1,(int)node.Tag);
33 
34                 }
35             }
36 
37    }
38 //處理根節(jié)點的子節(jié)點
39   private void RefreshChildNode(TreeView tr1,TreeNode treeNode, int parentId)
40  {
41          foreach (TreeNode node in tr1.Nodes)
42             {
43                 if((int)node.Tag==parentId)
44                 {
45                     node.Nodes.Add(treeNode);
46                     return;
47                 }else if (node.Nodes.Count > 0)
48                 {
49                     FindChildNode(node, treeNode,  parentId);
50                 }
51             }
52  }
53 
54   //處理根節(jié)點的子節(jié)點的子節(jié)點
55   private void FindChildNode(TreeNode  tNode,TreeNode treeNode, int parentId)
56  {
57          foreach (TreeNode node in tNode.Nodes)
58           {
59                 if ((int)node.Tag == parentId)
60                 {
61                     node.Nodes.Add(treeNode);
62                     return;
63                 }else if (node.Nodes.Count > 0)
64                 {
65                     FindChildNode(node,treeNode,parentId);
66                 }
67 
68             }
69 
70 }

復制代碼

3.4、遞歸刪除節(jié)點,,這里指的是遞歸刪除節(jié)點在數(shù)據(jù)表中的信息

復制代碼

 1  private void btnDelete_Click(object sender, EventArgs e)
 2    {
 3          if(treeView1.SelectedNode==null)
 4          {
 5                 MessageBox.Show("請選擇要刪除的節(jié)點,!");
 6                 return;
 7            }
 8             //選中節(jié)點的id,也是其子節(jié)點的parentId
 9             int id = (int)treeView1.SelectedNode.Tag;
10             nodeDelete(id);//遞歸刪除數(shù)據(jù)表中的數(shù)據(jù)
11             treeView1.SelectedNode.Remove();//刪除控件中的節(jié)點
12 
13   }
14    //數(shù)據(jù)表中的數(shù)據(jù)的遞歸刪除方法
15    public void nodeDelete(int id)
16  {
17             string sql = "select * from TreeTest where parentId="+id;
18             DataTable ds = sqlHelper.ExecuteDataTable(sql);
19             if (ds.Rows.Count > 0)
20             {
21                 //有子節(jié)點
22                 foreach(DataRow row in ds.Rows)
23                 {
24                     //先刪除父節(jié)點
25                     string delete = "delete from TreeTest where id=" + id;
26                     int k = sqlHelper.ExecuteNonQuery(delete);
27                     //查找子節(jié)點,,刪除
28                     int id1 = (int)row["id"];
29                     nodeDelete(id1);
30                 }
31             }
32             else
33             { 
34                //沒有子節(jié)點
35                 string delete = "delete from TreeTest where id="+id;
36                 int k = sqlHelper.ExecuteNonQuery(delete);
37             }
38 }

復制代碼

4,、TreeView控件的右鍵操作

TreeView控件的右鍵操作,需要兩個winform窗體form3(圖4-1),,form4(圖4-2),,其中form3中放置treeview1控件,兩個contextMenuStrip,contextMenuStrip1和contextMenuStrip2,,

form4用于彈出菜單 填寫要添加節(jié)點名稱

 

圖4-1

圖4-2

contextMenuStrip1

contextMenuStrip2

4.1 Form4窗體的代碼如下:

復制代碼

 1 public partial class Form4 : Form
 2  {
 3         public string nodeName
 4         {
 5             get { return textBox1.Text.Trim(); }
 6         }
 7         public Form4()
 8         {
 9             InitializeComponent();
10         }
11 
12         private void btnCancel_Click(object sender, EventArgs e)
13         {
14             DialogResult = DialogResult.Cancel;
15         }
16 
17         private void btnConfirm_Click(object sender, EventArgs e)
18         {
19             if(string.IsNullOrEmpty(textBox1.Text.Trim()))
20             {
21                 MessageBox.Show("請?zhí)顚懝?jié)點名稱,!");
22                 return;
23             }
24             DialogResult = DialogResult.OK;
25         }
26 }

復制代碼

4.2 form3窗體的代碼如下:

復制代碼

 1 public partial class Form3 : Form
 2     {
 3         public Form3()
 4         {
 5             InitializeComponent();
 6         }
 7         private void treeView1_MouseDown(object sender, MouseEventArgs e)
 8         {
 9             if(e.Button==MouseButtons.Right)
10             {
11                 Point ClickPoint = new Point(e.X, e.Y);
12                 int x = e.X;
13                 int y = e.Y;
14                 TreeNode CurrentNode = treeView1.GetNodeAt(ClickPoint);
15                 if (CurrentNode is TreeNode)//判斷你點的是不是一個節(jié)點
16                 {               
17                     treeView1.SelectedNode = CurrentNode;
18                     CurrentNode.ContextMenuStrip = this.contextMenuStrip1;                    
19                     contextMenuStrip1.Show(MousePosition);
20                 }
21                 else
22                 {
23                     treeView1.ContextMenuStrip = this.contextMenuStrip2;
24                     contextMenuStrip2.Show(MousePosition);
25                 }
26             }
27         }
28 
29         private void 添加子節(jié)點ToolStripMenuItem_Click(object sender, EventArgs e)
30         {
31             Form4 f5 = new Form4();
32             if (f5.ShowDialog() == DialogResult.OK)
33             {
34               treeView1.SelectedNode.Nodes.Add(f5.nodeName);
35             }
36         }
37 
38         private void 刪除選中節(jié)點ToolStripMenuItem_Click(object sender, EventArgs e)
39         {
40             treeView1.SelectedNode.Remove();
41         }
42 
43         private void 添加根節(jié)點ToolStripMenuItem1_Click(object sender, EventArgs e)
44         {
45             Form4 f4 = new Form4();
46             if (f4.ShowDialog() == DialogResult.OK)
47             {
48                 treeView1.Nodes.Add(f4.nodeName);
49             }
50         }
51 
52         private void 清空ToolStripMenuItem1_Click(object sender, EventArgs e)
53         {
54             treeView1.Nodes.Clear();
55         }
56     }

復制代碼

 4.3、示例中用到的sqlHelper.cs文件代碼如下所示

復制代碼

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Data;
  6 using System.Data.SqlClient;
  7 using System.Windows.Forms;
  8 
  9 namespace CRMProject.DAL
 10 {
 11     class SqlHelper
 12     {
 13         static DataTable  dtInfo = new DataTable();    
 14         public static SqlConnection My_con;  //定義一個sqlConnection類型的公共變量My_con,,用于判斷數(shù)據(jù)庫是否連接成功
 15 
 16 
 17         public static readonly string connstr = 
 18             ConfigurationManager.ConnectionStrings["dbconnstr"].ConnectionString;
 19 
 20  public static int ExecuteNonQuery(string cmdText,
 21             params SqlParameter[] parameters)
 22         {
 23             using (SqlConnection conn = new SqlConnection(connstr))
 24             {
 25                 conn.Open();
 26                 using (SqlCommand cmd = conn.CreateCommand())
 27                 {
 28                     cmd.CommandText = cmdText;
 29                     cmd.CommandTimeout = 3000;
 30                     cmd.Parameters.AddRange(parameters);
 31                     return cmd.ExecuteNonQuery();
 32                 }
 33             }
 34         }
 35 
 36   public static void dgrd_Connection(string strSql, DataGridView dgrd, int start, int pagesize, string tableName)
 37         {
 38 
 39             try
 40             {
 41                 SqlDataAdapter adapter = new SqlDataAdapter(strSql, getcon());
 42                 DataSet dataSet = new DataSet();
 43                 adapter.Fill(dataSet, start, pagesize, tableName);
 44                 dtInfo = dataSet.Tables[tableName];
 45                 dgrd.DataSource = dataSet.Tables[tableName];
 46                 dgrd.AllowUserToAddRows = false;
 47 
 48             }
 49             catch { }
 50         }
 51 
 52         public static SqlConnection getcon()
 53         {
 54 
 55 
 56             My_con = new SqlConnection(connstr);   //用SqlConnection對象與指定的數(shù)據(jù)庫相連接
 57             My_con.Open();  //打開數(shù)據(jù)庫連接
 58             return My_con;  //返回SqlConnection對象的信息              
 59 
 60 
 61         }
 62         public static int Count(string strSql)
 63         {
 64             using (SqlConnection conn = new SqlConnection(connstr))
 65             {
 66                 conn.Open();
 67                 using (SqlCommand cmd = conn.CreateCommand())
 68                 {
 69                     try
 70                     {
 71                         cmd.CommandText = strSql;
 72                         cmd.CommandTimeout = 3000;
 73                         int n = int.Parse(cmd.ExecuteScalar().ToString());
 74                         return n;
 75 
 76                     }
 77                     catch { return 0; }
 78                 }
 79             }
 80         }
 81         public static object ExecuteScalar(string cmdText,
 82             params SqlParameter[] parameters)
 83         {
 84             using (SqlConnection conn = new SqlConnection(connstr))
 85             {
 86                 conn.Open();
 87                 using (SqlCommand cmd = conn.CreateCommand())
 88                 {
 89                     cmd.CommandText = cmdText;
 90                     cmd.CommandTimeout = 3000;
 91                     cmd.Parameters.AddRange(parameters);
 92                     return cmd.ExecuteScalar();
 93                 }
 94             }
 95         }
 96 
 97         public static DataTable ExecuteDataTable(string cmdText,
 98             params SqlParameter[] parameters)
 99         {
100             using (SqlConnection conn = new SqlConnection(connstr))
101             {
102                 conn.Open();
103                 using (SqlCommand cmd = conn.CreateCommand())
104                 {
105                     cmd.CommandText = cmdText;
106                     cmd.CommandTimeout = 3000;
107                     cmd.Parameters.AddRange(parameters);
108                     using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
109                     {
110                         DataTable dt = new DataTable();
111                         adapter.Fill(dt);
112                         return dt;
113                     }
114                 }
115             }
116         }
117 
118         public static SqlDataReader ExecuteDataReader(string cmdText,
119             params SqlParameter[] parameters)
120         {
121             SqlConnection conn = new SqlConnection(connstr);
122             conn.Open();
123             using (SqlCommand cmd = conn.CreateCommand())
124             {
125                 cmd.CommandText = cmdText;
126                 cmd.CommandTimeout = 3000;
127                 cmd.Parameters.AddRange(parameters);
128                 return cmd.ExecuteReader(CommandBehavior.CloseConnection);
129             }
130         }
131     }
132 }

復制代碼

 5,、源碼下載地址

鏈接:http://pan.baidu.com/s/1mi5DGi0 密碼:t22u

 

from:https://www.cnblogs.com/net064/p/5534697.html

 

 

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,,不代表本站觀點,。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,,謹防詐騙,。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報,。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多