对《treeview的非常规绑定》中的第一种显示的改进,以及第二种的显示方法
Posted on 2007-08-11 00:08 (NineTyNine) 阅读(540) 评论(0) 编辑 收藏 举报
在这次改进中,减少了对数据库的操作。每个treeview 的绑定只对数据库进行了三次操作。即对三个表的读取。以加快浏览速度。
写了一个类,
SqlOperate.cs
以下是页面代码:
TreeView3是第一种显示方法,TreeView1是第二种显示方法
写了一个类,
SqlOperate.cs
1using System;
2using System.Data;
3using System.Data.SqlClient;
4using System.Configuration;
5
6
7namespace ForProjectTest
8{
9 /// <summary>
10 /// SqlOperate 的摘要说明。
11 /// </summary>
12 public class SqlOperate
13 {
14 public SqlOperate()
15 {
16 //
17 // TODO: 在此处添加构造函数逻辑
18 //
19 }
20
21
22
23 protected static string SqlConnection=ConfigurationSettings.AppSettings["con"];
24
25 /// <summary>
26 ///
27 /// </summary>
28 /// <param name="SqlString"></param>
29 /// <returns></returns>
30 public static DataSet ds(string SqlString)
31 {
32 SqlConnection con = new SqlConnection(SqlConnection);
33 SqlDataAdapter sda = new SqlDataAdapter();
34 sda.SelectCommand = new SqlCommand(SqlString,con);
35 DataSet ds = new DataSet();
36 sda.Fill(ds);
37 con.Close();
38 con.Dispose();
39 sda.Dispose();
40 return ds;
41 }
42
43
44 /// <summary>
45 /// 旧表里符合条件的行组合成新表(旧表只限于三列)
46 /// </summary>
47 /// <param name="OldDT">旧表</param>
48 /// <param name="liehao">列的索引</param>
49 /// <param name="tiaojian">字段值</param>
50 /// <returns></returns>
51 public static DataTable RNewDT(DataTable OldDT,int liehao,string tiaojian)
52 {
53 DataTable NewDT=new DataTable();
54 NewDT.Columns.Add("class_dep_code");
55 NewDT.Columns.Add("class_name");
56 NewDT.Columns.Add("class_code");
57 int RowCount = OldDT.Rows.Count;
58 for(int i=0;i<RowCount;i++)
59 {
60 string cellText = OldDT.Rows[i][liehao].ToString();
61 if(cellText==tiaojian)
62 {
63 DataRow NewRow =NewDT.NewRow();
64 NewRow["class_dep_code"]=OldDT.Rows[i][0].ToString();
65 string aa = OldDT.Rows[i][0].ToString();
66
67 NewRow["class_name"]=OldDT.Rows[i][1].ToString();
68 string bb = OldDT.Rows[i][1].ToString();
69
70 NewRow["class_code"]=OldDT.Rows[i][2].ToString();
71 string cc = OldDT.Rows[i][2].ToString();
72
73 NewDT.Rows.Add(NewRow);
74
75 }
76 }
77 return NewDT;
78 }
79
80 /// <summary>
81 /// 判断是否包含二级节点
82 /// </summary>
83 /// <param name="dt">表</param>
84 /// <param name="tiaojian">字段值</param>
85 /// <returns></returns>
86 public static bool IsNotConclude(DataTable dt,string tiaojian)
87 {
88 int RowCount = dt.Rows.Count;
89 string all=null;
90
91 for(int i=0;i<RowCount;i++)
92 {
93 string cellText = dt.Rows[i][0].ToString();
94 if(tiaojian==cellText)
95 {
96 all=all+cellText;
97 }
98 }
99 if(all==null)
100 {
101 return false;
102 }
103 else
104 {
105 return true;
106 }
107 }
108
109 /// <summary>
110 /// 查询单个数值
111 /// </summary>
112 /// <param name="SqlString">sql语句</param>
113 /// <returns></returns>
114 public static string FirstString(string SqlString)
115 {
116 SqlConnection con = new SqlConnection(SqlConnection);
117 con.Open();
118 SqlCommand cmd = new SqlCommand(SqlString,con);
119 string frist = Convert.ToString(cmd.ExecuteScalar());
120 con.Close();
121 con.Dispose();
122 return frist;
123 }
124
125 /// <summary>
126 /// 查询单个数值
127 /// </summary>
128 /// <param name="SqlString">sql语句</param>
129 /// <returns></returns>
130 public static int FirstInt(string SqlString)
131 {
132 SqlConnection con = new SqlConnection(SqlConnection);
133 con.Open();
134 SqlCommand cmd = new SqlCommand(SqlString,con);
135 Object fristObj = cmd.ExecuteScalar();
136 con.Close();
137 con.Dispose();
138 if(fristObj==DBNull.Value)
139 {
140 return 0;
141 }
142 else
143 {
144 return Convert.ToInt32(fristObj);
145 }
146 }
147
148
149
150 }
151}
152
2using System.Data;
3using System.Data.SqlClient;
4using System.Configuration;
5
6
7namespace ForProjectTest
8{
9 /// <summary>
10 /// SqlOperate 的摘要说明。
11 /// </summary>
12 public class SqlOperate
13 {
14 public SqlOperate()
15 {
16 //
17 // TODO: 在此处添加构造函数逻辑
18 //
19 }
20
21
22
23 protected static string SqlConnection=ConfigurationSettings.AppSettings["con"];
24
25 /// <summary>
26 ///
27 /// </summary>
28 /// <param name="SqlString"></param>
29 /// <returns></returns>
30 public static DataSet ds(string SqlString)
31 {
32 SqlConnection con = new SqlConnection(SqlConnection);
33 SqlDataAdapter sda = new SqlDataAdapter();
34 sda.SelectCommand = new SqlCommand(SqlString,con);
35 DataSet ds = new DataSet();
36 sda.Fill(ds);
37 con.Close();
38 con.Dispose();
39 sda.Dispose();
40 return ds;
41 }
42
43
44 /// <summary>
45 /// 旧表里符合条件的行组合成新表(旧表只限于三列)
46 /// </summary>
47 /// <param name="OldDT">旧表</param>
48 /// <param name="liehao">列的索引</param>
49 /// <param name="tiaojian">字段值</param>
50 /// <returns></returns>
51 public static DataTable RNewDT(DataTable OldDT,int liehao,string tiaojian)
52 {
53 DataTable NewDT=new DataTable();
54 NewDT.Columns.Add("class_dep_code");
55 NewDT.Columns.Add("class_name");
56 NewDT.Columns.Add("class_code");
57 int RowCount = OldDT.Rows.Count;
58 for(int i=0;i<RowCount;i++)
59 {
60 string cellText = OldDT.Rows[i][liehao].ToString();
61 if(cellText==tiaojian)
62 {
63 DataRow NewRow =NewDT.NewRow();
64 NewRow["class_dep_code"]=OldDT.Rows[i][0].ToString();
65 string aa = OldDT.Rows[i][0].ToString();
66
67 NewRow["class_name"]=OldDT.Rows[i][1].ToString();
68 string bb = OldDT.Rows[i][1].ToString();
69
70 NewRow["class_code"]=OldDT.Rows[i][2].ToString();
71 string cc = OldDT.Rows[i][2].ToString();
72
73 NewDT.Rows.Add(NewRow);
74
75 }
76 }
77 return NewDT;
78 }
79
80 /// <summary>
81 /// 判断是否包含二级节点
82 /// </summary>
83 /// <param name="dt">表</param>
84 /// <param name="tiaojian">字段值</param>
85 /// <returns></returns>
86 public static bool IsNotConclude(DataTable dt,string tiaojian)
87 {
88 int RowCount = dt.Rows.Count;
89 string all=null;
90
91 for(int i=0;i<RowCount;i++)
92 {
93 string cellText = dt.Rows[i][0].ToString();
94 if(tiaojian==cellText)
95 {
96 all=all+cellText;
97 }
98 }
99 if(all==null)
100 {
101 return false;
102 }
103 else
104 {
105 return true;
106 }
107 }
108
109 /// <summary>
110 /// 查询单个数值
111 /// </summary>
112 /// <param name="SqlString">sql语句</param>
113 /// <returns></returns>
114 public static string FirstString(string SqlString)
115 {
116 SqlConnection con = new SqlConnection(SqlConnection);
117 con.Open();
118 SqlCommand cmd = new SqlCommand(SqlString,con);
119 string frist = Convert.ToString(cmd.ExecuteScalar());
120 con.Close();
121 con.Dispose();
122 return frist;
123 }
124
125 /// <summary>
126 /// 查询单个数值
127 /// </summary>
128 /// <param name="SqlString">sql语句</param>
129 /// <returns></returns>
130 public static int FirstInt(string SqlString)
131 {
132 SqlConnection con = new SqlConnection(SqlConnection);
133 con.Open();
134 SqlCommand cmd = new SqlCommand(SqlString,con);
135 Object fristObj = cmd.ExecuteScalar();
136 con.Close();
137 con.Dispose();
138 if(fristObj==DBNull.Value)
139 {
140 return 0;
141 }
142 else
143 {
144 return Convert.ToInt32(fristObj);
145 }
146 }
147
148
149
150 }
151}
152
以下是页面代码:
TreeView3是第一种显示方法,TreeView1是第二种显示方法
1using System;
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Web;
7using System.Web.SessionState;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11using Microsoft.Web.UI.WebControls;
12
13using System.Data.SqlClient;
14
15namespace ForProjectTest
16{
17 /// <summary>
18 /// TreeviewBind 的摘要说明。
19 /// </summary>
20 public class TreeviewBind : System.Web.UI.Page
21 {
22 protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
23
24 protected Microsoft.Web.UI.WebControls.TreeView TreeView3;
25
26
27 private void Page_Load(object sender, System.EventArgs e)
28 {
29
30 // 在此处放置用户代码以初始化页面
31 }
32
33
34
35 Web 窗体设计器生成的代码
57
58
59 private void TreeView3_PreRender(object sender, System.EventArgs e)
60 {
61 TreeNode tmpNd;//一级节点声明
62 TreeNode tmpNd2;//二级节点声明
63 TreeNode tmpNd3;//三级节点(包含于二级节点中)
64 TreeNode tmpNd4;//三级节点(不包含于二级节点)
65
66
67 string sqlSelect = "select dep_name,dep_code from db_dep";
68 DataView dv =SqlOperate.ds(sqlSelect).Tables[0].DefaultView;
69 string sqlSelect2 = "select unit_name,unit_code from db_unit";
70 DataView dv3 = SqlOperate.ds(sqlSelect2).Tables[0].DefaultView;
71 //把db_class表存入内存中
72 string sqlSelect3 = "select class_dep_code,class_name,class_code from db_class";
73 DataTable dt = SqlOperate.ds(sqlSelect3).Tables[0];
74 // 用于添加一级节点的foreach语句
75 foreach(DataRowView drv in dv)
76 {
77 //给每个一级节点赋值
78
79 tmpNd = new TreeNode();
80 tmpNd.ID=drv["dep_code"].ToString();
81 tmpNd.Text=drv["dep_name"].ToString();
82 //判断当前节点是否存在二级节点,如果存在则增加二级节点,并添加三级节点
83 if(SqlOperate.IsNotConclude(dt,tmpNd.ID.ToString()))
84 {
85 //添加二级节点的foreach语句
86
87 DataView dv2 = SqlOperate.RNewDT(dt,0,tmpNd.ID.ToString()).DefaultView;
88 foreach(DataRowView drv2 in dv2)
89 {
90 tmpNd2 = new TreeNode();
91 tmpNd2.ID=drv2["class_code"].ToString();
92 tmpNd2.Text=drv2["class_name"].ToString();
93 //添加三级节点的foreach语句
94
95 foreach(DataRowView drv3 in dv3)
96 {
97 tmpNd3 = new TreeNode();
98 tmpNd3.ID = drv3["unit_code"].ToString();
99 tmpNd3.Text = drv3["unit_name"].ToString();
100 tmpNd2.Nodes.Add(tmpNd3);
101 }
102
103 //添加二级节点
104 tmpNd.Nodes.Add(tmpNd2);
105 }
106
107 }
108 //如果不存在,直接添加三级节点(实际上在此层是二级节点)
109 else
110 {
111 foreach(DataRowView drv3 in dv3)
112 {
113 tmpNd4 = new TreeNode();
114 tmpNd4.ID = drv3["unit_code"].ToString();
115 tmpNd4.Text = drv3["unit_name"].ToString();
116 tmpNd.Nodes.Add(tmpNd4);
117 }
118 }
119 //添加一级节点
120 this.TreeView3.Nodes.Add(tmpNd);
121
122 }
123
124 }
125
126 private void TreeView1_PreRender(object sender, System.EventArgs e)
127 {
128 TreeNode tmpNd;//一级节点的声明;
129 TreeNode tmpNd2;//二级节点的声明;
130 TreeNode tmpNd4;//三级节点的声明;
131
132 //添加一级节点
133 string SqlString = "select unit_code,unit_name from db_unit";
134 DataView dv = SqlOperate.ds(SqlString).Tables[0].DefaultView;
135
136 string SqlString2 = "select dep_code,dep_name from db_dep";
137 DataView dv2 = SqlOperate.ds(SqlString2).Tables[0].DefaultView;
138
139 string SqlString3 = "select class_dep_code,class_name,class_code from db_class";
140 DataTable dt = SqlOperate.ds(SqlString3).Tables[0];
141
142
143 foreach(DataRowView drv in dv)
144 {
145 tmpNd = new TreeNode();
146 tmpNd.ID = drv["unit_code"].ToString();
147 tmpNd.Text = drv["unit_name"].ToString();
148 //添加二级节点
149
150 foreach(DataRowView drv2 in dv2)
151 {
152 tmpNd2 = new TreeNode();
153 tmpNd2.ID = drv2["dep_code"].ToString();
154 tmpNd2.Text = drv2["dep_name"].ToString();
155
156
157 if(SqlOperate.IsNotConclude(dt,tmpNd2.ID.ToString()))
158 {
159 //添加三级节点
160 DataView dv4 = SqlOperate.RNewDT(dt,0,tmpNd2.ID.ToString()).DefaultView;
161 foreach(DataRowView drv4 in dv4)
162 {
163 tmpNd4 = new TreeNode();
164 tmpNd4.ID = drv4["class_code"].ToString();
165 tmpNd4.Text = drv4["class_name"].ToString();
166 tmpNd2.Nodes.Add(tmpNd4);
167 }
168 }
169 tmpNd.Nodes.Add(tmpNd2);
170 }
171 this.TreeView1.Nodes.Add(tmpNd);
172 }
173
174 }
175 }
176}
177
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Web;
7using System.Web.SessionState;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11using Microsoft.Web.UI.WebControls;
12
13using System.Data.SqlClient;
14
15namespace ForProjectTest
16{
17 /// <summary>
18 /// TreeviewBind 的摘要说明。
19 /// </summary>
20 public class TreeviewBind : System.Web.UI.Page
21 {
22 protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
23
24 protected Microsoft.Web.UI.WebControls.TreeView TreeView3;
25
26
27 private void Page_Load(object sender, System.EventArgs e)
28 {
29
30 // 在此处放置用户代码以初始化页面
31 }
32
33
34
35 Web 窗体设计器生成的代码
57
58
59 private void TreeView3_PreRender(object sender, System.EventArgs e)
60 {
61 TreeNode tmpNd;//一级节点声明
62 TreeNode tmpNd2;//二级节点声明
63 TreeNode tmpNd3;//三级节点(包含于二级节点中)
64 TreeNode tmpNd4;//三级节点(不包含于二级节点)
65
66
67 string sqlSelect = "select dep_name,dep_code from db_dep";
68 DataView dv =SqlOperate.ds(sqlSelect).Tables[0].DefaultView;
69 string sqlSelect2 = "select unit_name,unit_code from db_unit";
70 DataView dv3 = SqlOperate.ds(sqlSelect2).Tables[0].DefaultView;
71 //把db_class表存入内存中
72 string sqlSelect3 = "select class_dep_code,class_name,class_code from db_class";
73 DataTable dt = SqlOperate.ds(sqlSelect3).Tables[0];
74 // 用于添加一级节点的foreach语句
75 foreach(DataRowView drv in dv)
76 {
77 //给每个一级节点赋值
78
79 tmpNd = new TreeNode();
80 tmpNd.ID=drv["dep_code"].ToString();
81 tmpNd.Text=drv["dep_name"].ToString();
82 //判断当前节点是否存在二级节点,如果存在则增加二级节点,并添加三级节点
83 if(SqlOperate.IsNotConclude(dt,tmpNd.ID.ToString()))
84 {
85 //添加二级节点的foreach语句
86
87 DataView dv2 = SqlOperate.RNewDT(dt,0,tmpNd.ID.ToString()).DefaultView;
88 foreach(DataRowView drv2 in dv2)
89 {
90 tmpNd2 = new TreeNode();
91 tmpNd2.ID=drv2["class_code"].ToString();
92 tmpNd2.Text=drv2["class_name"].ToString();
93 //添加三级节点的foreach语句
94
95 foreach(DataRowView drv3 in dv3)
96 {
97 tmpNd3 = new TreeNode();
98 tmpNd3.ID = drv3["unit_code"].ToString();
99 tmpNd3.Text = drv3["unit_name"].ToString();
100 tmpNd2.Nodes.Add(tmpNd3);
101 }
102
103 //添加二级节点
104 tmpNd.Nodes.Add(tmpNd2);
105 }
106
107 }
108 //如果不存在,直接添加三级节点(实际上在此层是二级节点)
109 else
110 {
111 foreach(DataRowView drv3 in dv3)
112 {
113 tmpNd4 = new TreeNode();
114 tmpNd4.ID = drv3["unit_code"].ToString();
115 tmpNd4.Text = drv3["unit_name"].ToString();
116 tmpNd.Nodes.Add(tmpNd4);
117 }
118 }
119 //添加一级节点
120 this.TreeView3.Nodes.Add(tmpNd);
121
122 }
123
124 }
125
126 private void TreeView1_PreRender(object sender, System.EventArgs e)
127 {
128 TreeNode tmpNd;//一级节点的声明;
129 TreeNode tmpNd2;//二级节点的声明;
130 TreeNode tmpNd4;//三级节点的声明;
131
132 //添加一级节点
133 string SqlString = "select unit_code,unit_name from db_unit";
134 DataView dv = SqlOperate.ds(SqlString).Tables[0].DefaultView;
135
136 string SqlString2 = "select dep_code,dep_name from db_dep";
137 DataView dv2 = SqlOperate.ds(SqlString2).Tables[0].DefaultView;
138
139 string SqlString3 = "select class_dep_code,class_name,class_code from db_class";
140 DataTable dt = SqlOperate.ds(SqlString3).Tables[0];
141
142
143 foreach(DataRowView drv in dv)
144 {
145 tmpNd = new TreeNode();
146 tmpNd.ID = drv["unit_code"].ToString();
147 tmpNd.Text = drv["unit_name"].ToString();
148 //添加二级节点
149
150 foreach(DataRowView drv2 in dv2)
151 {
152 tmpNd2 = new TreeNode();
153 tmpNd2.ID = drv2["dep_code"].ToString();
154 tmpNd2.Text = drv2["dep_name"].ToString();
155
156
157 if(SqlOperate.IsNotConclude(dt,tmpNd2.ID.ToString()))
158 {
159 //添加三级节点
160 DataView dv4 = SqlOperate.RNewDT(dt,0,tmpNd2.ID.ToString()).DefaultView;
161 foreach(DataRowView drv4 in dv4)
162 {
163 tmpNd4 = new TreeNode();
164 tmpNd4.ID = drv4["class_code"].ToString();
165 tmpNd4.Text = drv4["class_name"].ToString();
166 tmpNd2.Nodes.Add(tmpNd4);
167 }
168 }
169 tmpNd.Nodes.Add(tmpNd2);
170 }
171 this.TreeView1.Nodes.Add(tmpNd);
172 }
173
174 }
175 }
176}
177