Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms 将DataGridView绑定到";中的实体;“主细节”;方式_Winforms_Entity Framework - Fatal编程技术网

Winforms 将DataGridView绑定到";中的实体;“主细节”;方式

Winforms 将DataGridView绑定到";中的实体;“主细节”;方式,winforms,entity-framework,Winforms,Entity Framework,我试图在两个DataGridView之间建立一个主细节关系。 我有一个EntityModel,其中有两个实体通过“ClientCommissions”关联连接。 它们是从现有数据库生成的,并且具有导航属性,运行良好。 证明(使用上述EntityModel的控制台应用程序): 但我无法在windows窗体上以主-详细方式绑定两个DataGridView: 我知道有一个BindingContext,它必须使用CurrencyManager完成所有工作,而不需要手工编写代码 我在这里呆了很久了

我试图在两个DataGridView之间建立一个主细节关系。 我有一个EntityModel,其中有两个实体通过“ClientCommissions”关联连接。 它们是从现有数据库生成的,并且具有导航属性,运行良好。 证明(使用上述EntityModel的控制台应用程序):



但我无法在windows窗体上以主-详细方式绑定两个DataGridView:



我知道有一个BindingContext,它必须使用CurrencyManager完成所有工作,而不需要手工编写代码

我在这里呆了很久了。请帮忙


UPD:

这段代码只为网格中的第一个客户端加载一次Commission。
但当我更改客户端网格中的当前行时,将不再查询DB,并且NavCommissionsGrid始终显示第一个客户端的Commission:(

在一个表单上拉取两个列表视图,分别命名为lstcontegory和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;
        }

    }       

DataSet和EntityDataModel之间有一个区别。它们都是.NET中数据访问的好技术。类型化DataSet是一个旧技术,现在是ADO.NET实体框架(EF)是新的。至于原因,EF可以更好地处理数据库中的关系。此外,它可以利用LINQ技术的优势,使用更方便。但是,这不应该成为您的拦路虎?我已经强调了您在工作原型中提出的问题的解决方案。现在,您应该能够导出自己的解决方案这个概念的解决方案。感谢您的努力,但我仍然试图找到一种使用EF和WinForms的方法。我知道如何使用数据集。至于我的调查结果,没有直接的方法可以“无代码”主细节绑定到EF。有一个开源项目可以做到这一点。伙计!为什么要投否决票…提供一个解决方案!
        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?";
        }
    }
        private void AnswerFromStackRefactored()
    {
        using (var context = new MnxEntities())
        {
            clientBindingSource.DataSource = context;
            clientBindingSource.DataMember = "Clients";

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

    }
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;
        }

    }