如何使用数据绑定在不同的表上创建 Treeview
How to have a Treeview on different tables using data binding
我想创建一个树视图,第一级绑定到 "Customer" (id int, name nvarchar, surname nvarchar) 并且子节点绑定到其他表 "Order" (customer_id int, id int, o_date date, code nvarchar) 和另一个 "Cases" (customer_id int, id int, c_date date, issue nvarchar).
如果在客户中我有
ID Name Surname
1 Joe Doe
2 Jim White
并按顺序
Customer_ID ID o_date code
1 1 01/01/2019 o001
1 2 01/01/2019 o002
2 3 01/01/2019 o003
案例
Customer_ID ID c_date code
2 1 01/01/2019 issue 001
我希望看到
Joe Doe
- o001
- o002
Jim White
- o003
+ issue 001
您需要加入table。也可能需要使用 Left Outer Join。参见:https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable customerTable = new DataTable();
customerTable.Columns.Add("ID", typeof(int));
customerTable.Columns.Add("Name", typeof(string));
customerTable.Columns.Add("Surname", typeof(string));
customerTable.Rows.Add(new object[] {1, "Joe", "Doe"});
customerTable.Rows.Add(new object[] {2, "Jim", "White"});
DataTable orderTable = new DataTable();
orderTable.Columns.Add("Customer_ID", typeof(int));
orderTable.Columns.Add("ID", typeof(int));
orderTable.Columns.Add("o_date", typeof(DateTime));
orderTable.Columns.Add("code", typeof(string));
orderTable.Rows.Add(new object[] {1, 1, DateTime.Parse("01/01/2019"), "o001"});
orderTable.Rows.Add(new object[] {1, 2, DateTime.Parse("01/01/2019"), "o002"});
orderTable.Rows.Add(new object[] {2, 3, DateTime.Parse("01/01/2019"), "o003"});
DataTable caseTable = new DataTable();
caseTable.Columns.Add("Customer_ID", typeof(int));
caseTable.Columns.Add("ID", typeof(int));
caseTable.Columns.Add("o_date", typeof(DateTime));
caseTable.Columns.Add("code", typeof(string));
caseTable.Rows.Add(new object[] {2, 1, DateTime.Parse("01/01/2019"), "issue 001"});
var results = (from cust in customerTable.AsEnumerable()
join order in orderTable.AsEnumerable() on cust.Field<int>("ID") equals order.Field<int>("ID")
join caseT in caseTable.AsEnumerable() on cust.Field<int>("ID") equals caseT.Field<int>("ID")
select new { cust = cust, order = order, caseT = caseT })
.ToList();
}
}
}
我想创建一个树视图,第一级绑定到 "Customer" (id int, name nvarchar, surname nvarchar) 并且子节点绑定到其他表 "Order" (customer_id int, id int, o_date date, code nvarchar) 和另一个 "Cases" (customer_id int, id int, c_date date, issue nvarchar).
如果在客户中我有
ID Name Surname
1 Joe Doe
2 Jim White
并按顺序
Customer_ID ID o_date code
1 1 01/01/2019 o001
1 2 01/01/2019 o002
2 3 01/01/2019 o003
案例
Customer_ID ID c_date code
2 1 01/01/2019 issue 001
我希望看到
Joe Doe
- o001
- o002
Jim White
- o003
+ issue 001
您需要加入table。也可能需要使用 Left Outer Join。参见:https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable customerTable = new DataTable();
customerTable.Columns.Add("ID", typeof(int));
customerTable.Columns.Add("Name", typeof(string));
customerTable.Columns.Add("Surname", typeof(string));
customerTable.Rows.Add(new object[] {1, "Joe", "Doe"});
customerTable.Rows.Add(new object[] {2, "Jim", "White"});
DataTable orderTable = new DataTable();
orderTable.Columns.Add("Customer_ID", typeof(int));
orderTable.Columns.Add("ID", typeof(int));
orderTable.Columns.Add("o_date", typeof(DateTime));
orderTable.Columns.Add("code", typeof(string));
orderTable.Rows.Add(new object[] {1, 1, DateTime.Parse("01/01/2019"), "o001"});
orderTable.Rows.Add(new object[] {1, 2, DateTime.Parse("01/01/2019"), "o002"});
orderTable.Rows.Add(new object[] {2, 3, DateTime.Parse("01/01/2019"), "o003"});
DataTable caseTable = new DataTable();
caseTable.Columns.Add("Customer_ID", typeof(int));
caseTable.Columns.Add("ID", typeof(int));
caseTable.Columns.Add("o_date", typeof(DateTime));
caseTable.Columns.Add("code", typeof(string));
caseTable.Rows.Add(new object[] {2, 1, DateTime.Parse("01/01/2019"), "issue 001"});
var results = (from cust in customerTable.AsEnumerable()
join order in orderTable.AsEnumerable() on cust.Field<int>("ID") equals order.Field<int>("ID")
join caseT in caseTable.AsEnumerable() on cust.Field<int>("ID") equals caseT.Field<int>("ID")
select new { cust = cust, order = order, caseT = caseT })
.ToList();
}
}
}