我正在尝试在两个DataGridView之间建立主从细节关系。 我有一个EntityModel,其中两个实体由“ClientComissions”关联连接。 它们是从现有数据库生成的,并具有导航属性,运行良好。 证明(使用提到的EntityModel的控制台应用程序):
using (var context = new MnxEntities())
{
Client client = context.Clients.FirstOrDefault();
// profiler: "SELECT TOP (1) ... FROM [Clients] AS [c]" - Ok!
Console.WriteLine("Client: {0}", client.Name);
foreach (Comission comission in client.NavComissions)
// profiler: "SELECT ... FROM [Comissions] WHERE [StateCode] = '20971504'" - Ok!
{
Console.WriteLine("Agreement number: {0}", comission.Dog_Num);
}
}
但我不能在Windows窗体上以主从细节方式绑定两个DataGridView:
private void tabComissions_Enter(object sender, EventArgs e)
{
using (var context = new MnxEntities())
{
clientDataGridView.DataSource = context.Clients;
comissionsDataGridView.DataSource = clientDataGridView.DataSource;
comissionsDataGridView.DataMember = "WHAT SHOULD BE HERE?";
}
}
我知道有一个BindingContext,必须使用CurrencyManager完成所有工作,不需要手写代码。
我已经坚持了很长时间。请帮助。
UPD:
private void AnswerFromStackRefactored()
{
using (var context = new MnxEntities())
{
clientBindingSource.DataSource = context;
clientBindingSource.DataMember = "Clients";
navComissionsBindingSource.DataSource = clientBindingSource;
navComissionsBindingSource.DataMember = "NavComissions";
}
}
此代码仅为网格中的第一个客户端加载一次Comissions。 但是当我更改客户端网格中的当前行时,不再有对DB的查询,并且navComissionsGrid始终显示第一个客户端的委托。 :(
答案 0 :(得分:1)
在表单上拉两个ListView,并将它们分别命名为lstcategory和lstProduct。然后复制下面的代码[非常简单]。您可以对您的问题应用相同的概念。
public partial class MasterDetail : Form
{
public MasterDetail()
{
InitializeComponent();
}
private BindingManagerBase categoryBinding;
private DataSet ds;
private void MasterDetail_Load(object sender, EventArgs e)
{
ds = GetCategoriesAndProducts();
// Bind the lists to different tables.
lstCategory.DataSource = ds.Tables["Categories"];
lstCategory.DisplayMember = "CategoryName";
lstProduct.DataSource = ds.Tables["Products"];
lstProduct.DisplayMember = "ProductName";
// Track the binding context and handle position changing.
categoryBinding = this.BindingContext[ds.Tables["Categories"]];
categoryBinding.PositionChanged += new EventHandler(Binding_PositionChanged);
// Update child table at startup.
UpdateProducts();
}
private void Binding_PositionChanged(object sender, System.EventArgs e)
{
UpdateProducts();
}
private void UpdateProducts()
{
string filter;
DataRow selectedRow;
// Find the current category row.
selectedRow = ds.Tables["Categories"].Rows[categoryBinding.Position];
// Create a filter expression using its CategoryID.
filter = "CategoryID='" + selectedRow["CategoryID"].ToString() + "'";
// Modify the view onto the product table.
ds.Tables["Products"].DefaultView.RowFilter = filter;
}
public DataSet GetCategoriesAndProducts()
{
DataTable category = new DataTable("Categories");
category.Columns.Add("CategoryID");
category.Columns.Add("CategoryName");
category.Rows.Add(new object[] { "1", "Food" });
category.Rows.Add(new object[] { "2", "Beverage" });
DataTable product = new DataTable("Products");
product.Columns.Add("CategoryID");
product.Columns.Add("ProductName");
product.Rows.Add(new object[] { "1", "Rice" });
product.Rows.Add(new object[] { "1", "Pasta" });
product.Rows.Add(new object[] { "2", "Cola" });
product.Rows.Add(new object[] { "2", "Coffee" });
product.Rows.Add(new object[] { "2", "Tea" });
DataSet ds = new DataSet();
ds.Tables.Add(category);
ds.Tables.Add(product);
// Set up a relation between these tables (optional).
DataRelation relCategoryProduct = new DataRelation("CategoryProduct",
ds.Tables["Categories"].Columns["CategoryID"],
ds.Tables["Products"].Columns["CategoryID"]);
ds.Relations.Add(relCategoryProduct);
return ds;
}
}