Csharp/CSharp Tutorial/GUI Windows Forms/DataBinding ListBox
Версия от 18:31, 26 мая 2010; (обсуждение)
Bind ArrayList to ListBox
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; public class Employee{
public string ID; public string Name ; public Employee(string strName, string strID) { this.ID = strID; this.Name = strName; } public override string ToString() { return this.ID + " : " + this.Name; }
} public class ListBoxBindArrayList : System.Windows.Forms.Form {
private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.RadioButton rbAuthors; private System.Windows.Forms.RadioButton rbEmployees; private System.Windows.Forms.ListBox lb; private DataTable dataTable; private ArrayList Employees = new ArrayList(); private System.ruponentModel.Container components = null; public ListBoxBindArrayList() { InitializeComponent(); lb.Items.Add("A"); lb.Items.Add("B"); lb.Items.Add("C"); lb.Items.Add("D"); lb.Items.Add("E"); lb.SelectedIndex = 0; // populate the arraylist for later use. Employees.Add(new Employee("A", "1")); Employees.Add(new Employee("B", "2")); Employees.Add(new Employee("C", "3")); Employees.Add(new Employee("D", "4")); Employees.Add(new Employee("E", "5")); Employees.Add(new Employee("F", "6")); Employees.Add(new Employee("G", "7")); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.lb = new System.Windows.Forms.ListBox(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.rbEmployees = new System.Windows.Forms.RadioButton(); this.rbAuthors = new System.Windows.Forms.RadioButton(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // lb // this.lb.Location = new System.Drawing.Point(16, 8); this.lb.Name = "lb"; this.lb.Size = new System.Drawing.Size(232, 212); this.lb.TabIndex = 0; this.lb.DisplayMemberChanged += new System.EventHandler(this.lb_DisplayMemberChanged); this.lb.ValueMemberChanged += new System.EventHandler(this.lb_ValueMemberChanged); this.lb.DataSourceChanged += new System.EventHandler(this.lb_DataSourceChanged); this.lb.SelectedValueChanged += new System.EventHandler(this.lb_SelectedValueChanged); this.lb.SelectedIndexChanged += new System.EventHandler(this.lb_SelectedIndexChanged); // // groupBox1 // this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.rbEmployees, this.rbAuthors}); this.groupBox1.Location = new System.Drawing.Point(16, 240); this.groupBox1.Name = "groupBox1"; this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "DataSource"; // // rbEmployees // this.rbEmployees.Location = new System.Drawing.Point(24, 56); this.rbEmployees.Name = "rbEmployees"; this.rbEmployees.TabIndex = 1; this.rbEmployees.Text = "Employee\"s"; this.rbEmployees.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged); // // rbAuthors // this.rbAuthors.Checked = true; this.rbAuthors.Location = new System.Drawing.Point(24, 32); this.rbAuthors.Name = "rbAuthors"; this.rbAuthors.TabIndex = 0; this.rbAuthors.TabStop = true; this.rbAuthors.Text = "Authors"; this.rbAuthors.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged); // // ListBoxBindArrayList // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(264, 389); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.groupBox1, this.lb}); this.Name = "ListBoxBindArrayList"; this.Text = "ListBoxBindArrayList"; this.Load += new System.EventHandler(this.ListBoxBindArrayList_Load); this.groupBox1.ResumeLayout(false); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new ListBoxBindArrayList()); } private void rb_CheckedChanged(object sender, System.EventArgs e) { lb.DataSource = Employees; lb.DisplayMember = "Name"; lb.ValueMember = "ID"; } private void lb_SelectedIndexChanged(object sender, System.EventArgs e) { MessageBox.Show(lb.SelectedIndex.ToString()+ "\n" + lb.GetItemText(lb.SelectedItem),"lb_SelectedIndexChanged"); } private void lb_SelectedValueChanged(object sender, System.EventArgs e) { MessageBox.Show(lb.GetItemText(lb.SelectedItem),"lb_SelectedValueChanged"); } private void lb_DataSourceChanged(object sender, System.EventArgs e) { MessageBox.Show(lb.DataSource.ToString(), "lb_DataSourceChanged"); } private void lb_DisplayMemberChanged(object sender, System.EventArgs e) { MessageBox.Show(lb.DisplayMember.ToString(), "lb_DisplayMemberChanged"); } private void lb_ValueMemberChanged(object sender, System.EventArgs e) { MessageBox.Show(lb.ValueMember.ToString(), "lb_ValueMemberChanged"); } private void ListBoxBindArrayList_Load(object sender, System.EventArgs e) { this.lb.SelectedValueChanged += new System.EventHandler(this.lb_SelectedValueChanged); this.lb.SelectedIndexChanged += new System.EventHandler(this.lb_SelectedIndexChanged); }
}</source>
Master detail view
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; 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.rumandText = 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"]); } } }</source>