// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Windows.Forms; namespace DefaultNamespace { public class T1 { private int f1; private string f2; public T1() { f1 = 0; f2 = ""; } public T1(int f1, string f2) { this.f1 = f1; this.f2 = f2; } public int F1 { get { return f1; } set { f1 = value; } } public string F2 { get { return f2; } set { f2 = value; } } } public class MainForm : Form { private ComboBox comboBox1; public MainForm() { InitializeComponent(); } [STAThread] public static void Main(string[] args) { Application.Run(new MainForm()); } private void InitializeComponent() { comboBox1 = new ComboBox(); SuspendLayout(); comboBox1.Location = new System.Drawing.Point(50, 50); comboBox1.Size = new System.Drawing.Size(150, 25); OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Database\\MSAccess\\Test.mdb"); con.Open(); OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM T1", con); DataSet ds = new DataSet(); da.Fill(ds, "T1"); comboBox1.Items.Add(new T1(0, "")); for(int i = 0; i < ds.Tables["T1"].Rows.Count; i++) { DataRow row = ds.Tables["T1"].Rows[i]; comboBox1.Items.Add(new T1((int)row["F1"], (string)row["F2"])); } comboBox1.DisplayMember = "F2"; comboBox1.ValueMember = "F1"; ClientSize = new Size(250, 400); Controls.Add(comboBox1); Text = "Combobox test"; ResumeLayout(false); } } }