Populating ComboBoxDataColumn items and values

Posted by MarceloRamires on Stack Overflow See other posts from Stack Overflow or by MarceloRamires
Published on 2010-05-18T19:41:47Z Indexed on 2010/06/07 14:32 UTC
Read the original article Hit count: 457

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET