views:

175

answers:

1

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

+3  A: 

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; }
    }
}
Charles
You don't sound obnoxious at all.Everyone knows the .NET framework as well as C# are heavily documented, and I don't want to sound ingrate, but i'd calculate that about 60 thousand of the 81 thousand questions about c# were from people that have read the documentation, didn't understand it, misinterpreted it, or haven't read it at all. I'm among these, I've read some about it, but haven't figured out easily how to do it, checked out on S.O. and found no question about it, then I decided to make a question about it, and maybe other people can find it in the future, but thank you!
MarceloRamires
@MarceloRamires: You are probably right. Does the documentation I quoted help? There are some examples on that page too. If you're still confused I could whip up a simpler example for you.
Charles
I'd be glad =) I've updated my question
MarceloRamires
+1 To you both (question and answer) for this nice communication. I appreciate that! =)
Will Marcouiller
@MarceloRamires: what was the problem with my example?
Charles