C#中Master Details主从网格多查询传递与子实体如何绑定
C# WinForm 主从数据网格绑定实现指南
核心逻辑
现有Dapper查询拿到的三个平铺实体集合,不能直接把person、product绑定给子网格实现联动。WinForm主从绑定的核心是通过层级化的BindingSource建立关联,让系统自动根据主网格选中行匹配对应的子项,无需手写行切换的筛选逻辑。
分步实现步骤
1. 前期准备
确保三个实体的字段匹配关联要求:
- 主表
companyentity包含主键字段(比如Id,类型为int) - 子表
personEntity、productentity包含和主表主键对应的外键字段(比如CompanyId,类型和主表主键完全一致) - 在设计器中拖入3个BindingSource组件,分别命名为
tblsherkatBindingSource(主表用)、personBindingSource(人员子表用)、productBindingSource(产品子表用),两个子DataGridView分别绑定对应的子BindingSource。
2. 两种可选绑定实现
方案一:通过内存DataRelation实现(无需修改实体结构)
通过实体生成内存DataTable,在内存数据集建立主外键关联,适配现有从Dapper读取的无导航属性的平铺实体:
using (IDbConnection db = new SqlConnection("Data Source =.; Initial Catalog = tejarat_iran; Integrated Security = True")) { var multiReport = db.QueryMultiple( "SearchReport", new { search = IdCompany, kind = 0 }, commandType: CommandType.StoredProcedure); // 读取多结果集 var companyList = multiReport.Read<Class_entity.companyentity>().ToList(); var personList = multiReport.Read<Class_entity.personEntity>().ToList(); var productList = multiReport.Read<Class_entity.productentity>().ToList(); // 实体转内存DataTable(可自行实现泛型扩展方法,通过反射遍历实体属性生成表结构) DataTable dtCompany = companyList.ToDataTable(); dtCompany.TableName = "Company"; DataTable dtPerson = personList.ToDataTable(); dtPerson.TableName = "Person"; DataTable dtProduct = productList.ToDataTable(); dtProduct.TableName = "Product"; // 构建内存数据集,添加主外键关系 DataSet ds = new DataSet(); ds.Tables.AddRange(new[] { dtCompany, dtPerson, dtProduct }); // 公司-人员关联 DataRelation relPerson = new DataRelation( "Company_Person_Relation", dtCompany.Columns["Id"], dtPerson.Columns["CompanyId"]); ds.Relations.Add(relPerson); // 公司-产品关联 DataRelation relProduct = new DataRelation( "Company_Product_Relation", dtCompany.Columns["Id"], dtProduct.Columns["CompanyId"]); ds.Relations.Add(relProduct); // 完成层级绑定 tblsherkatBindingSource.DataSource = ds; tblsherkatBindingSource.DataMember = "Company"; personBindingSource.DataSource = tblsherkatBindingSource; personBindingSource.DataMember = "Company_Person_Relation"; productBindingSource.DataSource = tblsherkatBindingSource; productBindingSource.DataMember = "Company_Product_Relation"; }
方案二:通过实体导航属性实现(轻量无需DataTable)
给主实体临时加导航属性,手动匹配关联子项,直接绑定实体集合:
- 给
companyentity补充两个导航属性:public class companyentity { // 原有实体字段... public List<personEntity> RelatedPersons { get; set; } public List<productentity> RelatedProducts { get; set; } } - 读取实体后手动完成关联匹配:
using (IDbConnection db = new SqlConnection("Data Source =.; Initial Catalog = tejarat_iran; Integrated Security = True")) { var multiReport = db.QueryMultiple( "SearchReport", new { search = IdCompany, kind = 0 }, commandType: CommandType.StoredProcedure); var companyList = multiReport.Read<Class_entity.companyentity>().ToList(); var personList = multiReport.Read<Class_entity.personEntity>().ToList(); var productList = multiReport.Read<Class_entity.productentity>().ToList(); // 遍历匹配关联子项 foreach (var company in companyList) { company.RelatedPersons = personList.Where(p => p.CompanyId == company.Id).ToList(); company.RelatedProducts = productList.Where(p => p.CompanyId == company.Id).ToList(); } // 完成绑定 tblsherkatBindingSource.DataSource = companyList; personBindingSource.DataSource = tblsherkatBindingSource; personBindingSource.DataMember = "RelatedPersons"; productBindingSource.DataSource = tblsherkatBindingSource; productBindingSource.DataMember = "RelatedProducts"; }
常见踩坑点
- 不要直接把子网格的DataSource设为原始的
personList、productList,这种方式不会随主网格选中行联动,会显示全量数据。 - 关联的主外键字段类型必须完全匹配,比如主表
Id是int类型,子表的CompanyId不能是string类型,否则关联匹配失败,子网格会显示空数据。 - 不需要手动给主网格加
SelectedIndexChanged事件写筛选逻辑,绑定引擎会根据层级关联自动完成子项筛选。
内容的提问来源于stack exchange,提问作者Reyhaneh Sh
相关产品推荐
相关产品推荐

