Bind DataGridView to Entities in a “master-detail” manner

前端 未结 1 521
天命终不由人
天命终不由人 2021-01-27 22:57

I\'m trying to establish a master-detail relation between two DataGridView. I have an EntityModel with two entities connected by \"ClientComissions\" Association. They were gene

相关标签:
1条回答
  • 2021-01-27 23:24

    Pull two ListViews on a Form and name them as lstcategory and lstProduct respectively. Then copy the code below [its very simple]. You may apply the same concept to your problem.

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

    enter image description here

    0 讨论(0)
提交回复
热议问题