TreeView控件绑定数据库
1.在设计视图里面的代码
<form id="form1" runat="server"> <div> <h1>两个表</h1> <asp:TreeView ID="TreeView1" runat="server"> <HoverNodeStyle Font-Underline="true" ForeColor="Window" /> <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px" NodeSpacing="0px" VerticalPadding="2px"/> <ParentNodeStyle Font-Bold="False" /> <SelectedNodeStyle BackColor="Red" Font-Underline="False" HorizontalPadding="0px" VerticalPadding="0px" /> </asp:TreeView> <br /> <asp:Button ID="Button1" runat="server" Text="ShowTwoTable" OnClick="Button1_Click"/> <br /><br /><hr /> <h1>一个表</h1> <asp:TreeView ID="TreeView2" runat="server"></asp:TreeView> <br /> <asp:Button ID="Button2" runat="server" Text="ShowOne" OnClick ="Button2_Click" /> </div> </form>
2.后台代码:
两个关联表的显示
#region 两个表的显示 //根据获取的数据填充TreeView控件 private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode) { foreach (DataRow row in dtParent.Rows) { TreeNode child = new TreeNode { Text = row["Name"].ToString(), Value = row["Id"].ToString() }; if (parentId == 0) { TreeView1.Nodes.Add(child); DataTable dtChild = this.GetDataTableBySql("select Id,Name from VehicleSubType where VehicleId= " + child.Value); PopulateTreeView(dtChild, int.Parse(child.Value), child); } else { treeNode.ChildNodes.Add(child); } } } //根据sql语句从数据库中获取整个表的数据 private DataTable GetDataTableBySql(string sql) { DataTable dt = new DataTable(); string constr = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(sql)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.CommandType = CommandType.Text; cmd.Connection = con; sda.SelectCommand = cmd; sda.Fill(dt); } } return dt; } } protected void Button1_Click(object sender, EventArgs e) { DataTable dt = this.GetDataTableBySql("select Id, Name from VechicleType "); this.PopulateTreeView(dt, 0, null); } #endregion
一个表的显示:
#region 一个表的显示 protected void Button2_Click(object sender, EventArgs e) { PopulateMenu(); } //use the linq to sql MynodeDataContext db = new MynodeDataContext(); private void PopulateMenu() { List<NodeSubNode> allMenu = new List<NodeSubNode>(); allMenu = db.NodeSubNodes.ToList(); CreateTreeView(allMenu, 0, null); } private void CreateTreeView(List<NodeSubNode> source, int parentID, TreeNode parentNode) { List<NodeSubNode> newSource = db.NodeSubNodes.Where(a => a.SubjectId.Equals(parentID)).ToList(); foreach (var i in newSource) { TreeNode newnode = new TreeNode(i.Name, i.Id.ToString()); if (parentNode == null) { TreeView2.Nodes.Add(newnode); } else { parentNode.ChildNodes.Add(newnode); } CreateTreeView(source, i.Id, newnode); } } #endregion
显示效果:
More information:
http://dotnetawesome.blogspot.com/2013/11/how-to-bind-treeview-from-database.html
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/navigation/treeview.aspx
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。