c# - 填充 ComboBox DataColumn 项和值

标签 c# .net combobox

我有一个“填充组合框”,我对它非常满意,甚至开始使用更多组合框。它通过引用表中“值集”(或任何您想要的名称)的 ID 获取组合框对象,并添加项目及其各自的值(不同)并完成工作。

我最近有了一个在 GridView 中使用组合框的绝妙主意,我很高兴地注意到它的工作方式就像单个组合框一样,但同时填充给定列中的所有组合框。

ObjComboBox.Items.Add("yadayada");
//works just like
ObjComboBoxColumn.Items.Add("blablabla");

但是当我开始计划如何填充这些组合框时,我注意到:ComboBoxDataColumn 中没有“Values”属性。

ObjComboBox.Values = whateverArray;
//works, but the following doesn't
ObjComboBoxColumn.Values = whateverArray;

问题:
0 - 我如何填充它的值? (我怀疑它就这么简单,但使用了另一个名称)
1 - 如果它像组合框一样工作,那么没有此属性的解释是什么?


-----[编辑]------

所以我检查了查尔斯的引言,我想我必须改变我填充这些坏男孩的方式。我不应该循环遍历字符串并将它们一一插入到组合框中,而是应该抓取我想要在表中填充的字段,并将表的一列设置为“值”,另一列设置为“显示” 。所以我这样做了:

ObjComboBoxColumn.DataSource = DTConfig; //Double checked, guaranteed to be populated

ObjComboBoxColumn.ValueMember = "Code"; 
ObjComboBoxColumn.DisplayMember = "Description";

但是,如果我使用相同的对象,什么也不会发生:

ObjComboBoxColumn.Items.Add("StackOverflow");

已添加。

没有 DataBind() 函数。

它找到了两列,这是有保证的(“代码”和“描述”),如果我将它们的名称更改为不存在的名称,它会给我一个异常(exception),所以这是一个好兆头。


-----[编辑]------

我在 SQL Server 中有一个类似的表

代码  |文字
——————
1    |富
2    |酒吧

这很简单,并且通过其他组合框( GridView 之外),我已经成功地填充了循环行并添加文本:

ObjComboBox.Items.Add(MyDataTable.Rows[I]["MyColumnName"].ToString());

获取每个值,将其添加到数组中,然后将其设置为:

ObjComboBox.Values = MyArray;

我想像使用组合框一样简单地填充我的组合框列。

最佳答案

我不想听起来令人讨厌,但你知道所有这些东西都有文档吗?

来自http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.aspx :

You can populate the column drop-down list manually by adding values to the Items collection. Alternatively, you can bind the drop-down list to its own data source by setting the column DataSource property. If the values are objects in a collection or records in a database table, you must also set the DisplayMember and ValueMember properties. The DisplayMember property indicates which object property or database column provides the values that are displayed in the drop-down list. The ValueMember property indicates which object property or database column is used to set the cell Value property.


编辑:

根据您的编辑,听起来您可能正在尝试使用 DisplayMember 和/或 ValueMember 基础类型的非公共(public)属性。或者,如果您的组合框数据源是数据表,请确保它具有“代码”和“描述”列。

这是一个简单的演示。我创建了 Foo 列表并将其指定为组合框列的数据源。只需创建一个 winforms 应用程序并将其粘贴进去即可。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }                

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        // this will be the datasource for the combo box column; you could also bind it to a dataset
        List<Foo> foos = new List<Foo>() {
            new Foo() { FooID = 0, FooName = "No Foo." }, 
            new Foo() { FooID = 1, FooName = "Foo Me Once" },
            new Foo() { FooID = 2, FooName = "Foo Me Twice" },
            new Foo() { FooID = 3, FooName = "Pity The Foo!" }
        };

        DataGridView dataGridView1 = new DataGridView();
        dataGridView1.AutoGenerateColumns = false;

        // add normal text column
        DataGridViewColumn column = new DataGridViewTextBoxColumn();
        column.DataPropertyName = "MyText";
        column.Name = "Text";
        dataGridView1.Columns.Add(column);

        // add the combo box column 
        DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn();
        comboCol.Name = "Foo";
        // bind it to the list of foos to populate it
        comboCol.DataSource = foos;
        // specify which property of the grid's datasource to bind 
        comboCol.DataPropertyName = "MyFoo";
        // specify the property of the combo's datasource to bind 
        comboCol.ValueMember = "FooID";
        // specify the property of the combo's datasource to display
        comboCol.DisplayMember = "FooName";

        dataGridView1.Columns.Add(comboCol);

        // add some data
        BindingSource bindingSource1 = new BindingSource();
        bindingSource1.Add(new BusinessObject(1, "You say"));
        bindingSource1.Add(new BusinessObject(2, "George says"));
        bindingSource1.Add(new BusinessObject(3, "Mr. T says"));
        bindingSource1.Add(new BusinessObject());
        dataGridView1.DataSource = bindingSource1;

        Controls.Add(dataGridView1);
        dataGridView1.Dock = DockStyle.Fill;
    }        

    class Foo
    {
        public int FooID { get; set; }
        public string FooName { get; set; }        
    }

    class BusinessObject
    {
        public BusinessObject(int foo, string text)
        {
            MyFoo = foo;
            MyText = text;
        }
        public BusinessObject()
        {
            MyFoo = 0;
            MyText = "";
        }            
        public string MyText { get; set; }
        public int MyFoo { get; set; }
    }
}

关于c# - 填充 ComboBox DataColumn 项和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2860523/

相关文章:

c# - 在 WCF 中使用 HttpClient 强制关闭连接

.net - apache ignite .net 计划任务

.net - 在绑定(bind)到字典的组合框中设置所选项目

python - tkinter ttk.Combobox 下拉/展开并聚焦于文本

c# - 2 个 ObservableCollections 1 个组合框 WPF

c# - 使用 Moq 时在 Dapper 方法上获取 NotSupportedException

c# - 很棒的 ASP.NET 和 C# 初学者教程

c# - Windbg:无法遍历托管堆栈

c# - Process.start:如何获得输出?

c# - 将 MahApps 图标与 ContextMenu 结合使用