c# - C#中组合框的使用方法

标签 c# combobox

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

我什么都试过了

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

    comboBox1.DataSource = test.XXXX ;

}

最佳答案

假设您指的是 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);

(特别是,SelectedValue 应该为您提供 123456 - 对于 ID 等很有用)

关于c# - C#中组合框的使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1304416/

相关文章:

c# - 世界货币查找表 - 带有 ISO 3166-1 alpha-2 代码?

c# - 从 Word 文档转换为 HTML

c# - 将枚举序列化为字符串

c# - 无法以编程方式设置 WPF 的 ComboBox 项文本

JavaFX:使用 MySQL 数据库中的数据填充组合框,StringConverter 破坏组合框

c# - 在 asp.net C# 中重定向按钮单击上的新选项卡(Response.Redirect)

c# - 在 .NET 中提取图像的一部分

c# - GridView 组合框数据绑定(bind) WPF

c# - 无法在不崩溃的情况下从 ComboBox 中删除项目

WPF 组合框验证