我的客户在他的电子商务数据库中有两张表
No(PK), PropertyName
和
No(PK), ProductNo(FK), PropertyNo(FK), Value
他只是想让我像这样制作一张桌子
+-----------+-------------+--------------+------------+
| | Property 1 | Property 2 | .. all properties-> |
+-----------+-------------+--------------+------------+
| Product1 | x | 4 | x |
| Product2 | 2 | x | 1 |
| Product3 | x | x | x |
| ... | | | |
| (all products) | | |
+----+-------------+---------------------+------------+
我试图通过中继器制作,但我不能。我怎样才能实现它?
我放弃并以@Bala R的形式制作解决方案 但是有点变化......
示例类
public class list {
public int No { get; set; }
public string PropertyName { get; set; }
}
public class list2 {
public int ProductNo { get; set; }
public int PropertyNo { get; set; }
public int Value { get; set; }
}
public class list3 {
public string ProductName { get; set; }
public int No { get; set; }
}
样本列表,
List<list> propertyList = new List<list>();
List<list2> propertyProductList = new List<list2>();
List<list3> productList = new List<list3>();
propertyList.Add(new list { No = 1, PropertyName= "Property 1" });
propertyList.Add(new list { No = 2, PropertyName = "Property 2" });
propertyList.Add(new list { No = 3, PropertyName = "Property 3" });
propertyList.Add(new list { No = 4, PropertyName = "Property 4" });
propertyProductList.Add(new list2 { ProductNo = 1, PropertyNo = 1, Value = 3 });
propertyProductList.Add(new list2 { ProductNo = 2, PropertyNo = 3, Value = 13 });
propertyProductList.Add(new list2 { ProductNo = 2, PropertyNo = 2, Value = 8 });
propertyProductList.Add(new list2 { ProductNo = 3, PropertyNo = 2, Value = 6 });
propertyProductList.Add(new list2 { ProductNo = 4, PropertyNo = 1, Value = 2 });
propertyProductList.Add(new list2 { ProductNo = 3, PropertyNo = 1, Value = 55 });
productList.Add(new list3 { No = 1, ProductName = "Ball" });
productList.Add(new list3 { No = 2, ProductName = "Book" });
productList.Add(new list3 { No = 3, ProductName = "Pencil" });
productList.Add(new list3 { No = 4, ProductName = "TV" });
和解决方案,
var resultSet = (from c in list2
group c by c.ProductNo into g
select new {
ProductNo = g.Key,
Value = g
}).ToList();
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Products"));
foreach (var item in list) {
dt.Columns.Add(new DataColumn() { ColumnName = item.PropertyName });
}
foreach (var item in resultSet) {
DataRow dr = dt.NewRow();
dr["Products"] = list3.First(p=> p.No== item.ProductNo).ProductName;
foreach (var item2 in item.Value) {
dr[list.First(l=>l.No == item2.PropertyNo).PropertyName] = item2.Value;
}
dt.Rows.Add(dr);
}
dataGrid1.DataSource = dt;
dataGrid1.DataBind();
答案 0 :(得分:1)
您可以创建这样的类
class Product
{
public int ProductNo { get; set; }
public string ProductName { get; set; }
}
class Property
{
public int PropertyNo { get; set; }
public string PropertyName { get; set; }
}
class Value
{
public int ProductPropertyNo { get; set; }
public int ProductNo { get; set; }
public int PropertyNo { get; set; }
public string Value { get; set; }
}
并加载枚举
IEnumerable<Product> products = GetProducts();
IEnumerable<Property> properties = GetProperties();
IEnumerable<Value> values = GetValues();
为DataTable执行类似的操作
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ProductName"));
foreach (var propNo in values.Select(v => v.PropertyNo).Distinct())
{
dt.Columns.Add(
new DataColumn(properties.Where(p => p.PropertyNo == propNo).First().PropertyName));
}
foreach (var prodNo in values.Select(v => v.ProductNo).Distinct())
{
Product prod = products.Where(p => p.ProductNo == prodNo).First();
DataRow dr = dt.NewRow();
dr["ProductName"] = prod.ProductName;
foreach (var value in values.Where(v => v.ProductNo == prodNo))
{
Property prop = properties.Where(p => p.PropertyNo == value.PropertyNo).First();
dr[prop.PropertyName] = value.Value;
}
}
答案 1 :(得分:0)
我不是DataTable
的忠实粉丝,但在这种情况下,这是实现这一目标的最简单方法。首先创建一个“产品”列,然后为每个属性在DataTable
上创建一个新列。
为您的所有产品添加DataRows
,并在相应的属性列中填写值。
然后将DataTable
绑定到DataGrid
。