您的位置首页生活百科

treeview 绑定数据库

treeview 绑定数据库

干吗用XML,费半天劲解析了,再存进XML,再交给程序,让CLR再解析一遍,没必要啊。直接边解析,边操纳行凯作TreeView===============================SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString); string collMcName = "select ToCollegeName,MajorName from MajorT"; SqlCommand cmd = new SqlCommand(collMcName, conn); try { conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); //禁止刷带锋新 TreeView1.BeginUpdate(); while (dr.Read()) { //洞唤添加第一层TreeNode TreeNode tn = new TreeNode((string)dr["MajorName"]); //添加第二层 tn.Nodes.Add((string)dr["ToCollegeName"]); //添加到TreeView TreeView1.Nodes.Add(tn); } TreeView1.EndUpdate(); } catch (SqlException ex) { Response.Write(ex.Message); } finally { conn.Close(); }