ASP.NET 树视图超出 Sql 绑定
ASP.NET Tree View out of Sql Binding
我得到了一个类别 Table,其中有一列用于我的数据库中的树视图,我想将其显示为树视图。
我的 TreeView 列如下所示。 (希望你明白)
0001.
0001.0001.
0001.0002.
0001.0002.0001.
0001.0002.0002.
0001.0003.
0002.
...
...
...
这可以显示在 asp:TreeView 中吗?或者我可以用什么其他方式来显示它?
数据库已经存在我只是在设计网站,不能在数据库中做太多事情。
编辑:
我尝试了第一个解决方案,现在我明白了。
Image
但我不想显示号码,只是显示名称和号码,当我点击它时,我的值是不可见的。
尝试如下操作:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication64
{
public partial class Form1 : Form
{
DataTable dt = null;
public Form1()
{
InitializeComponent();
dt = new DataTable();
dt.Columns.Add("Index", typeof(string));
dt.Columns.Add("Value", typeof(string));
dt.Rows.Add(new object[] { "0001.", "aaaa"});
dt.Rows.Add(new object[] { "0001.0001.", "bbbb"});
dt.Rows.Add(new object[] { "0001.0002.", "cccc"});
dt.Rows.Add(new object[] { "0001.0002.0001.", "dddd"});
dt.Rows.Add(new object[] { "0001.0002.0002.", "eeee"});
dt.Rows.Add(new object[] { "0001.0003.", "ffff"});
dt.Rows.Add(new object[] { "0002.", "gggg" });
new Index(dt);
MakeTreeRecursive(0, Index.indexes, null);
treeView1.ExpandAll();
}
public void MakeTreeRecursive(int level, List<Index> rows, TreeNode parent)
{
TreeNode child = null;
var groups = rows
.OrderBy(x => x.paragraphs[level])
.GroupBy(x => x.paragraphs[level]);
foreach (var group in groups)
{
child = new TreeNode(group.Key);
if (parent == null)
{
treeView1.Nodes.Add(child);
}
else
{
parent.Nodes.Add(child);
}
foreach (Index node in group.Where(x => x.paragraphs.Count == level + 1))
{
child.Nodes.Add(node.row.Field<string>("Value"));
}
List<Index> descendants = group.Where(x => x.paragraphs.Count > level + 1).ToList();
if (descendants.Count > 0)
{
MakeTreeRecursive(level + 1, descendants, child);
}
}
}
}
public class Index
{
public static List<Index> indexes { get; set; }
public List<string> paragraphs { get; set; }
public DataRow row { get; set; }
public Index() { }
public Index(DataTable dt)
{
Index.indexes = dt.AsEnumerable()
.Select(x => new Index() {
row = x,
paragraphs = x.Field<string>("Index")
.Split(new char[] {'.'}, StringSplitOptions.RemoveEmptyEntries)
.Select(y=> y).ToList()
}).ToList();
}
}
}
如果您只想要文本,这里是解决方案
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
DataTable dt = null;
public Form1()
{
InitializeComponent();
dt = new DataTable();
dt.Columns.Add("Index", typeof(string));
dt.Columns.Add("Value", typeof(string));
dt.Rows.Add(new object[] { "0001.", "aaaa" });
dt.Rows.Add(new object[] { "0001.0001.", "bbbb" });
dt.Rows.Add(new object[] { "0001.0002.", "cccc" });
dt.Rows.Add(new object[] { "0001.0002.0001.", "dddd" });
dt.Rows.Add(new object[] { "0001.0002.0002.", "eeee" });
dt.Rows.Add(new object[] { "0001.0003.", "ffff" });
dt.Rows.Add(new object[] { "0002.", "gggg" });
new Index(dt);
MakeTreeRecursive(0, Index.indexes, null);
treeView1.ExpandAll();
}
public void MakeTreeRecursive(int level, List<Index> rows, TreeNode parent)
{
TreeNode child = null;
var groups = rows
.OrderBy(x => x.paragraphs[level])
.GroupBy(x => x.paragraphs[level]);
foreach (var group in groups)
{
Index root = group.OrderBy(x => x.paragraphs.Count).First();
child = new TreeNode(root.row.Field<string>("Value"));
List<Index> descendants = group.Where(x => x.paragraphs.Count > level + 1).ToList();
if (descendants.Count() > 0)
{
MakeTreeRecursive(level + 1, descendants, child);
}
if (parent == null)
{
treeView1.Nodes.Add(child);
}
else
{
parent.Nodes.Add(child);
}
}
}
}
public class Index
{
public static List<Index> indexes { get; set; }
public List<string> paragraphs { get; set; }
public DataRow row { get; set; }
public Index() { }
public Index(DataTable dt)
{
Index.indexes = dt.AsEnumerable()
.Select(x => new Index()
{
row = x,
paragraphs = x.Field<string>("Index")
.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries)
.Select(y => y).ToList()
}).ToList();
}
}
}
我得到了一个类别 Table,其中有一列用于我的数据库中的树视图,我想将其显示为树视图。
我的 TreeView 列如下所示。 (希望你明白)
0001.
0001.0001.
0001.0002.
0001.0002.0001.
0001.0002.0002.
0001.0003.
0002.
...
...
...
这可以显示在 asp:TreeView 中吗?或者我可以用什么其他方式来显示它?
数据库已经存在我只是在设计网站,不能在数据库中做太多事情。
编辑:
我尝试了第一个解决方案,现在我明白了。 Image
但我不想显示号码,只是显示名称和号码,当我点击它时,我的值是不可见的。
尝试如下操作:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication64
{
public partial class Form1 : Form
{
DataTable dt = null;
public Form1()
{
InitializeComponent();
dt = new DataTable();
dt.Columns.Add("Index", typeof(string));
dt.Columns.Add("Value", typeof(string));
dt.Rows.Add(new object[] { "0001.", "aaaa"});
dt.Rows.Add(new object[] { "0001.0001.", "bbbb"});
dt.Rows.Add(new object[] { "0001.0002.", "cccc"});
dt.Rows.Add(new object[] { "0001.0002.0001.", "dddd"});
dt.Rows.Add(new object[] { "0001.0002.0002.", "eeee"});
dt.Rows.Add(new object[] { "0001.0003.", "ffff"});
dt.Rows.Add(new object[] { "0002.", "gggg" });
new Index(dt);
MakeTreeRecursive(0, Index.indexes, null);
treeView1.ExpandAll();
}
public void MakeTreeRecursive(int level, List<Index> rows, TreeNode parent)
{
TreeNode child = null;
var groups = rows
.OrderBy(x => x.paragraphs[level])
.GroupBy(x => x.paragraphs[level]);
foreach (var group in groups)
{
child = new TreeNode(group.Key);
if (parent == null)
{
treeView1.Nodes.Add(child);
}
else
{
parent.Nodes.Add(child);
}
foreach (Index node in group.Where(x => x.paragraphs.Count == level + 1))
{
child.Nodes.Add(node.row.Field<string>("Value"));
}
List<Index> descendants = group.Where(x => x.paragraphs.Count > level + 1).ToList();
if (descendants.Count > 0)
{
MakeTreeRecursive(level + 1, descendants, child);
}
}
}
}
public class Index
{
public static List<Index> indexes { get; set; }
public List<string> paragraphs { get; set; }
public DataRow row { get; set; }
public Index() { }
public Index(DataTable dt)
{
Index.indexes = dt.AsEnumerable()
.Select(x => new Index() {
row = x,
paragraphs = x.Field<string>("Index")
.Split(new char[] {'.'}, StringSplitOptions.RemoveEmptyEntries)
.Select(y=> y).ToList()
}).ToList();
}
}
}
如果您只想要文本,这里是解决方案
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
DataTable dt = null;
public Form1()
{
InitializeComponent();
dt = new DataTable();
dt.Columns.Add("Index", typeof(string));
dt.Columns.Add("Value", typeof(string));
dt.Rows.Add(new object[] { "0001.", "aaaa" });
dt.Rows.Add(new object[] { "0001.0001.", "bbbb" });
dt.Rows.Add(new object[] { "0001.0002.", "cccc" });
dt.Rows.Add(new object[] { "0001.0002.0001.", "dddd" });
dt.Rows.Add(new object[] { "0001.0002.0002.", "eeee" });
dt.Rows.Add(new object[] { "0001.0003.", "ffff" });
dt.Rows.Add(new object[] { "0002.", "gggg" });
new Index(dt);
MakeTreeRecursive(0, Index.indexes, null);
treeView1.ExpandAll();
}
public void MakeTreeRecursive(int level, List<Index> rows, TreeNode parent)
{
TreeNode child = null;
var groups = rows
.OrderBy(x => x.paragraphs[level])
.GroupBy(x => x.paragraphs[level]);
foreach (var group in groups)
{
Index root = group.OrderBy(x => x.paragraphs.Count).First();
child = new TreeNode(root.row.Field<string>("Value"));
List<Index> descendants = group.Where(x => x.paragraphs.Count > level + 1).ToList();
if (descendants.Count() > 0)
{
MakeTreeRecursive(level + 1, descendants, child);
}
if (parent == null)
{
treeView1.Nodes.Add(child);
}
else
{
parent.Nodes.Add(child);
}
}
}
}
public class Index
{
public static List<Index> indexes { get; set; }
public List<string> paragraphs { get; set; }
public DataRow row { get; set; }
public Index() { }
public Index(DataTable dt)
{
Index.indexes = dt.AsEnumerable()
.Select(x => new Index()
{
row = x,
paragraphs = x.Field<string>("Index")
.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries)
.Select(y => y).ToList()
}).ToList();
}
}
}