Populating ComboBoxDataColumn Items and Values

I have a "populate combobox" and I am so happy that I even started using more combobox. It takes a combobox by reference with a "set of values" id (or whatever you want to call it) from the table and adds the items and their corresponding values ​​(which are different) and does the job.

I recently had a brilliant idea for using comboboxes in a gridview and was happy to notice that it worked JUST LIKE with one list, but filled all the comboboxes in a 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 noticed that there is no "Values" property in the ComboBoxDataColumn.

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

      

Questions:
0 - How can I fill in its values? (I suspect it is just as simple, but uses a different name)
1 - If it works the same as it does with a combobox, what explanation does it have for this attribute?


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

So I checked out Charles's quote and I decided that I should change my way of populating these bad boys. Instead of looping through the rows and inserting them one at a time into the combobox, I have to grab the fields I want to populate in the table and set one column of the table as "value" and the other as "display". So I did this:

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

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

      

But nothing happens if I use the same object:

ObjComboBoxColumn.Items.Add ("StackOverflow");

Added.

There is no DataBind () function.

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


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

I have a table in SQL Server, something like

code | text
-----
1 | Foo
2 | bar

It's simple, and with other comboboxes (outside of gridviews) I have successfully completed the row-by-row loop and added the texts:

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

      

And getting each value, adding it to the array and setting it like this:

ObjComboBox.Values = MyArray;

      

I would like to populate my comboboxColumns as easily as with comboboxes.

+2


a source to share


1 answer


I don't mean to sound obnoxious, but do you know the documentation for all of this?

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

You can fill in the dropdown list of columns manually by adding values ​​to Items . Alternatively, you can bind the dropdown to your own data source by setting the DataSource Property column. 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 specifies which property of the object or database column contains the values ​​that are displayed in the dropdown list box. The ValueMember property specifies which property of an object or database column is used for the Cell Value property.




EDIT:

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

Here's a simple demo. I am creating a list Foo and assigning it as the DataSource of the combobox column. Just create a winforms app and paste it.

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 datasource to bind 
        comboCol.DataPropertyName = "MyFoo";
        // specify the property of the combo datasource to bind 
        comboCol.ValueMember = "FooID";
        // specify the property of the combo 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; }
    }
}

      

+3


a source







All Articles