0

2 つの DataGridView 間にマスター/ディテール関係を確立しようとしています。「ClientComissions」アソシエーションによって接続された 2 つのエンティティを持つ EntityModel があります。それらは既存の DB から生成され、適切に機能するナビゲーション プロパティを備えています。証明 (前述の 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 フォームで 2 つの 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?";
        }
    }

手書きのコードを必要とせずに、CurrencyManager を使用してすべての作業を行う必要がある BindingContext があることは知っています。

私は長い間ここに立ち往生しています。助けてください。


更新:

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

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

    }

このコードは、グリッド内の最初のクライアントに対して一度だけコミッションをロードします。しかし、クライアント グリッドの現在の行を変更すると、DB へのクエリがなくなり、navComissionsGrid は常に最初のクライアントのコミッションを表示します。:(

4

1 に答える 1

1

フォームで 2 つの 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 に答える