Hi all,
I've made a blank solution ten add 2 class library projects:-
1- The first one called "DataAccessOjects" and contains a public class called "Customer_DAO" with 2 properties "ID, Name"
2- The second one called "DataAccess" and contains a public class called "Customer_DA" with one method called "AddCustomer" that takes an object from "Customer_DAO" - this is available after add reference from the first project into the second one -
Then I added a windows form project that contains a listbox...
Now I'd like to bind listbox to datasource of type object... How can I do that in windows form in c#???
I hope that my Q is clear
Ahmed Ayman YassinPosted Sep 13, 2009, 3:21 PM
private void Form1_Load(object sender, EventArgs e)
{
CustomerDAO cust = new CustomerDAO();
CustomerDataAccess custDA = new CustomerDataAccess();
listBox1.DataSource=custDA.GetAllCustomers();
listBox1.DisplayMember = cust.CustName;
listBox1.ValueMember = cust.CustID.ToString();
}
I've solved my problem using the above piece of code,but I'd like 2 know how 2 bind listbox to object in GUI
Roei BarPosted Sep 13, 2009, 3:15 PM
Ahmed Ayman YassinPosted Sep 13, 2009, 2:46 PM
Roei BarPosted Sep 13, 2009, 1:39 PM
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace DataBinding101
{
///
/// Summary description for ObjectListBinding2.
///
public class ObjectListBinding2 : System.Windows.Forms.Form
{
internal System.Windows.Forms.ListBox lstCustomers;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public ObjectListBinding2()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.
lstCustomers= new System.Windows.Forms.ListBox();this.SuspendLayout();
//
// lstCity
//
this.lstCity.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.
lstCustomers.IntegralHeight = false;this.
lstCustomers.Location = new System.Drawing.Point(4, 9);this.
lstCustomers.Name = "lstCustomers";this.
lstCustomers.Size = new System.Drawing.Size(248, 216);this.
lstCustomers.TabIndex = 3;
//
// ObjectListBinding2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
this.ClientSize = new System.Drawing.Size(260, 234);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.lstCity});
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Name = "ObjectListBinding2";
this.Text = "ObjectListBinding2";
this.Load += new System.EventHandler(this.ObjectListBinding2_Load);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new ObjectListBinding2());
}
private void ObjectListBinding2_Load(object sender, System.EventArgs e)
{
Customers[] _customersList = {new
Customer("blabla", "1"),new
Customer("blabla2", "2"),newCustomer("blabla3", "3"),new
Customer("blabla4", "4")};
lst
Customers.DataSource =customersList;
}
}
public class Customer
{
private string name;
private string id;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string ID
{
get
{
return id;
}
set
{
id = value;
}
}
public AddCustomer(string name, string ID)
{
this.Name = name;
this.ID= ID;
}
public override string ToString()
{
return ID + ", " + Name;
}
}
}