tags:

views:

189

answers:

4

I don't no idea where to start. i tried DataTable but it didn't work.(This is an easy question :) )

I tried everything

{
    var test = new DataTable();
    test.Columns.Add("test");
    test.TableName = "test";
    test.Columns.Add("test");

    comboBox1.DataSource = test.XXXX ;

}

Help please. Thanks.

+1  A: 

You'll need to set the 'DataItemField' and 'DataValueField' to the appropriate column names in your datatable.

Noon Silk
+2  A: 

ComboBox.Items property, unless you want data from a database or something.

Matthew Iselin
+6  A: 

Assuming you mean winforms, something like:

    DataTable test = new DataTable();
    test.TableName = "test";
    test.Columns.Add("foo", typeof(string));
    test.Columns.Add("bar", typeof(int));
    test.Rows.Add("abc", 123);
    test.Rows.Add("def", 456);

    ComboBox cbo = new ComboBox();
    cbo.DataSource = test;
    cbo.DisplayMember = "foo";
    cbo.ValueMember = "bar";

    Form form = new Form();
    form.Controls.Add(cbo);
    Application.Run(form);

(in particular, SelectedValue should give you the 123 and 456 - useful for ids, etc)

Marc Gravell
+2  A: 
  DataTable dt=new DataTable();
  dt.Columns.Add("Col1",typeof(int));
  dt.Columns.Add("Col2",typeof(String));
  dt.Rows.Add(1,"A");
  dt.Rows.Add(2,"B");

   comboBox1.DataSource = dt;
   comboBox1.DisplayMember = "Col2";
   comboBox1.ValueMember = "Col1";
adatapost