开发者

Populating ComboBoxDataColumn items and values

开发者 https://www.devze.com 2022-12-30 23:44 出处:网络
I have a \"populate combobox\", and I\'m so happy with it that I\'ve even started using more comboboxes. It takes the combobox object by reference with the ID of the \"value set\" (or whatever you wan

I have a "populate combobox", and I'm so happy with it that I've even started using more comboboxes. It takes the combobox object by reference with the ID of the "value set" (or whatever you want to call it) from a table and adds the items and their respective values (which differ) and does the job.

I've recently had the brilliant idea of using comboboxes in a gridview, and I was happy to notice that it worked JUST LIKE a single combobox, but populating all the comboboxes in the given column at the same time.

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

But When I started planning how to populate these comboboxes I've noticed: There's no "Values" property in ComboBoxDataColumn.

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

Questions:

0 - How do I populate it's values ? (I suspect it's just as simple, but uses another name)

1 - If it works just like a combobox, what's the explanation for not having this attribute ?


-----[EDIT]------

So I've checked out Charles' quote, and I've figured I had to change my way of populating these bad boys. Instead of looping through the strings and inserting them one by one in the combobox, I should grab the fields I want to populate in a table, and set one column of the table as the "value", and other one as the "display". So I've done this:

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

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

But nothing happens, if I use the same ob开发者_JAVA百科ject as so:

ObjComboBoxColumn.Items.Add("StackOverflow");

It is added.

There is no DataBind() function.

It finds the two columns, and that's guaranteed ("Code" and "Description") and if I change their names to nonexistant ones it gives me an exception, so that's a good sign.


-----[EDIT]------

I have a table in SQL Server that is something like

code  |  text

—————

   1    | foo

   2    | bar

It's simple, and with other comboboxes (outside of gridviews) i've successfully populated looping through the rows and adding the texts:

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

And getting every value, adding it into an array, and setting it like:

ObjComboBox.Values = MyArray;

I'd like to populate my comboboxColumns just as simply as I do with comboboxes.


I don't mean to sound obnoxious, but do you know there's documentation for all this stuff?

From 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.


EDIT:

From your edit, it sounds like you might be trying to use non-public properties of the underlying type for DisplayMember and/or ValueMember. Or if your combobox datasource is a DataTable, make sure it has "Code" and "Description" columns.

Here's a simple demo. I create a list of Foo's and assign it as the DataSource of my combobox column. Just create a winforms application and paste this in.

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; }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消