如何在c#中使用组合框

Ath*_*han 2 c# combobox

我不知道从哪里开始.我尝试了DataTable但它没有用.(这是一个简单的问题:))

我尝试了一切

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

    comboBox1.DataSource = test.XXXX ;

}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 6

假设你的意思是winforms,比如:

    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);
Run Code Online (Sandbox Code Playgroud)

(特别是,SelectedValue应该给你123456- 有用的ids等)