Mega Code Archive

 
Categories / C# Tutorial / GUI Windows Forms
 

Master detail view

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient;   public class MasterDetail : System.Windows.Forms.Form   {     public MasterDetail()     {       this.lstCategories = new System.Windows.Forms.ListBox();       this.lstProducts = new System.Windows.Forms.ListBox();       this.label1 = new System.Windows.Forms.Label();       this.label2 = new System.Windows.Forms.Label();       this.SuspendLayout();       //        this.lstCategories.Location = new System.Drawing.Point(8, 28);       this.lstCategories.Size = new System.Drawing.Size(140, 225);       this.lstCategories.TabIndex = 0;       this.lstCategories.SelectedIndexChanged += new System.EventHandler(this.lstCategories_SelectedIndexChanged);       //        this.lstProducts.Location = new System.Drawing.Point(152, 28);       this.lstProducts.Size = new System.Drawing.Size(252, 225);       this.lstProducts.TabIndex = 1;       //        this.label1.Location = new System.Drawing.Point(8, 8);       this.label1.Size = new System.Drawing.Size(108, 16);       this.label1.TabIndex = 2;       this.label1.Text = "Category:";       //        this.label2.Location = new System.Drawing.Point(152, 8);       this.label2.Size = new System.Drawing.Size(108, 16);       this.label2.TabIndex = 3;       this.label2.Text = "Products:";       //        this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);       this.ClientSize = new System.Drawing.Size(412, 266);       this.Controls.AddRange(new System.Windows.Forms.Control[] {                                       this.label2,                                       this.label1,                                       this.lstProducts,                                       this.lstCategories});       this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));       this.Text = "A Master-Detail Form";       this.Load += new System.EventHandler(this.MasterDetail_Load);       this.ResumeLayout(false);     }     string connectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI";     string categorySQL = "SELECT * FROM Categories";     string productSQL = "SELECT * FROM Products";     DataSet ds = new DataSet();     DataRelation relation;     private System.Windows.Forms.ListBox lstCategories;     private System.Windows.Forms.Label label1;     private System.Windows.Forms.Label label2;     private System.Windows.Forms.ListBox lstProducts;     public static void Main()     {       Application.Run(new MasterDetail());     }     private void MasterDetail_Load(object sender, System.EventArgs e)     {       SqlConnection con = new SqlConnection(connectionString);       SqlCommand com = new SqlCommand(categorySQL, con);       SqlDataAdapter adapter = new SqlDataAdapter(com);              con.Open();       adapter.Fill(ds, "Categories");       adapter.SelectCommand.CommandText = productSQL;       adapter.Fill(ds, "Products");       con.Close();       DataColumn parentCol = ds.Tables["Categories"].Columns["CategoryID"];       DataColumn childCol = ds.Tables["Products"].Columns["CategoryID"];       relation = new DataRelation("Cat_Prod", parentCol, childCol);       ds.Relations.Add(relation);       foreach (DataRow row in ds.Tables["Categories"].Rows)       {         lstCategories.Items.Add(row["CategoryName"]);       }     }     private void lstCategories_SelectedIndexChanged(object sender, System.EventArgs e)     {             lstProducts.Items.Clear();       DataRow[] rows = ds.Tables["Categories"].Select("CategoryName='" + lstCategories.Text + "'");       DataRow parent = rows[0];       foreach (DataRow child in parent.GetChildRows(relation))       {         lstProducts.Items.Add(child["ProductName"]);       }     }   }