How do I compare DataSourced ComboBox to a value in DataRow?

G

Guest

I seem to be having trouble trying to select the right value in a ComboBox
DataSourced by a DataTable with a value for a DataRow...

For example... In a Windows Form, I have a ComboBox:

ComboBox box = new ComboBox(whatever);
box.DataSource = myDataTable;
box.DataMember = "the_column";

// now select value already in a passed DataRow
DataRow row (the row that's passed in frmo somewhere)
box.SelectedItem = row["the_column"];

Doesn't work...

I tried many variations with TypeCasting and can't get it to work- what's
the secret here?
 
M

Mohamoss

Hi
You need to set both DisplayMemeber ( the column form within the datatable
that would be displayed on your form ) . and the ValueMember ( which is the
real column of the datatable that the combobox is bounded to . A classical
example would be the ID for valueMember while what is displayed is the Name
. then you use the valuemember when you want to set the selectedItem.
this.comboBox1.DisplayMember
this.comboBox1.ValueMember
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top