DevExpress 控件 gridcontrol控件做主从表,如何list1绑定到gridView1和list2绑定到gridView2?
DevExpress控件gridcontrol控件做主从表,如何list1绑定到gridView1和list2绑定到gridView2?通过点gridView1的<用户名...
DevExpress 控件 gridcontrol控件做主从表,如何list1绑定到gridView1和list2绑定到gridView2?通过点gridView1的<用户名>列展开明细表显示list2对应的gridView2?
展开
展开全部
1)记录和记录绑定列表类:
using System.ComponentModel;
namespace WindowsFormsApplication1
{
//主记录
public class MasterRecord
{
public string Name { get; set; }
public DetailRecordList DetailRecordList { get; set; }
}
//主记录列表(绑定列表)
public class MasterRecordList : BindingList<MasterRecord>
{
}
//从记录
public class DetailRecord
{
public string Course { get; set; }
public int Score { get; set; }
}
//从记录列表
public class DetailRecordList : BindingList<DetailRecord>
{
}
}
2)在Form1中
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private MasterRecordList dataSource;
public Form1()
{
InitializeComponent();
BuildDataSource();
//设置数据源
gridControl1.DataSource = dataSource;
}
//创建数据源
private void BuildDataSource()
{
dataSource = new MasterRecordList();
DetailRecordList detail = new DetailRecordList();
detail.Add(new DetailRecord() { Course = "数学", Score = 89 });
detail.Add(new DetailRecord() { Course = "外语", Score = 79 });
detail.Add(new DetailRecord() { Course = "化学", Score = 100 });
dataSource.Add(new MasterRecord() { Name = "张三", DetailRecordList = detail });
detail = new DetailRecordList();
detail.Add(new DetailRecord() { Course = "自动控制", Score = 89 });
detail.Add(new DetailRecord() { Course = "嵌入式系统", Score = 79 });
detail.Add(new DetailRecord() { Course = "测试技术", Score = 100 });
dataSource.Add(new MasterRecord() { Name = "李四", DetailRecordList = detail });
}
}
}
3)运行结果
追问
我通过EF从数据库里得到如下两个记录来实现主从表,怎么实现和你这样同样的效果
var msaterRecord = context.tb_OrderInfo.ToList();//tb_OrderInfo表有"客户名称","地址"等信息
var detailRecord = context.tb_OrderDetails.ToList();//tb_OrderDetails表有 产品型号,数量等信息
追答
实现思路是一样的。
public class Order
{
public string CustomerName{get; set;}
public string Address{get;set;}
public IList<Product> ProductList{get;set;}
}
public class Product
{
pubilc string ProductName {get; set;}
public int Quantity{get; set}
}
//-----在Form1中
List<Order> dataSource = new List<Order>();
BuildDataSource();
gridControl1.DataSource = dataSource;
private void BuildDataSource() {
var msaterRecord = context.tb_OrderInfo.ToList();//tb_OrderInfo
foreach(var r in masterRecord)
{
Order order = new Order( );
order.CustomerName = r.CustomerName;
order.Address = r.Address;
//此订单对应的产品列表
//从 var detailRecord 过滤出该订单对应的产品(可以用Linq)实现
var product = from p in detailRecord …… select……
order.ProductList = product.ToList();
dataSource.Add(order)
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询