0

我正在尝试在两个 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 完成所有工作,不需要手写代码。

我已经在这里呆了很长时间了。请帮忙。


升级版:

        private void AnswerFromStackRefactored()
    {
        using (var context = new MnxEntities())
        {
            clientBindingSource.DataSource = context;
            clientBindingSource.DataMember = "Clients";

            navComissionsBindingSource.DataSource = clientBindingSource;
            navComissionsBindingSource.DataMember = "NavComissions";
        }

    }

此代码仅为网格中的第一个客户端加载一次佣金。但是,当我更改客户端网格中的当前行时,不再查询数据库,并且 navComissionsGrid 始终显示第一个客户端的佣金。:(

4

1 回答 1

1

在 Form 上拉出两个 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;
        }

    }       

在此处输入图像描述

于 2012-04-26T16:00:22.973 回答