Master detail view : DataBinding ListBox « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » DataBinding ListBox 
23.81.2.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(828);
      this.lstCategories.Size = new System.Drawing.Size(140225);
      this.lstCategories.TabIndex = 0;
      this.lstCategories.SelectedIndexChanged += new System.EventHandler(this.lstCategories_SelectedIndexChanged);
      // 
      this.lstProducts.Location = new System.Drawing.Point(15228);
      this.lstProducts.Size = new System.Drawing.Size(252225);
      this.lstProducts.TabIndex = 1;
      // 
      this.label1.Location = new System.Drawing.Point(88);
      this.label1.Size = new System.Drawing.Size(10816);
      this.label1.TabIndex = 2;
      this.label1.Text = "Category:";
      // 
      this.label2.Location = new System.Drawing.Point(1528);
      this.label2.Size = new System.Drawing.Size(10816);
      this.label2.TabIndex = 3;
      this.label2.Text = "Products:";
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(514);
      this.ClientSize = new System.Drawing.Size(412266);
      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"]);
      }
    }
  }
23.81.DataBinding ListBox
23.81.1.Bind ArrayList to ListBox
23.81.2.Master detail view
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.