How to get value of 3rd column in combobox attached to a dataset t

G

Guest

I created a table in a dataset that contains multiple columns. I attached a
combo box to that table. Now I'm doing something wrong with trying to
retrieve the value of the third column of the selecteditem.

The datatable row contains the following columns, (ProductCd, Description,
ProductType), the displaymember is Description, and the valuemember is
ProductCd.

I know how to get the values of the displaymember and valuemember, but how
do I get the value of the ProductType for the selecteditem?

Thanks
Vern
 
G

Guest

I may have found the answers to my own question.

If the combobox is attached to a datatable, then the following seems to work:
DataTable table = (DataTable)combobox1.DataSource;
string textfield =
table.Rows[combobox1.SelectedIndex]["column_name].ToString();

If the combobox is attached to a dataview:
DataView view = (DataView)combobox1.DataSource;
string textfield = view[combobox1.SelectedIndex]["column_name"].ToString();

Let me know if there is a better way.
 
G

Guest

I was shown in another newsgroup, a more direct way to get the answer. I had
tried to do this, but wasn't able to get the correct syntax. Anyway, the
combobox stores each row in an object that is retrievable using the
selecteditem property. Here's the code that maps the selecteditem to a
datarowview so it can extract the column data.

sField1.Text =
((DataRowView)sCombobox1.SelectedItem)["ColumnName"].ToString();


Vern said:
I may have found the answers to my own question.

If the combobox is attached to a datatable, then the following seems to work:
DataTable table = (DataTable)combobox1.DataSource;
string textfield =
table.Rows[combobox1.SelectedIndex]["column_name].ToString();

If the combobox is attached to a dataview:
DataView view = (DataView)combobox1.DataSource;
string textfield = view[combobox1.SelectedIndex]["column_name"].ToString();

Let me know if there is a better way.


Vern said:
I created a table in a dataset that contains multiple columns. I attached a
combo box to that table. Now I'm doing something wrong with trying to
retrieve the value of the third column of the selecteditem.

The datatable row contains the following columns, (ProductCd, Description,
ProductType), the displaymember is Description, and the valuemember is
ProductCd.

I know how to get the values of the displaymember and valuemember, but how
do I get the value of the ProductType for the selecteditem?

Thanks
Vern
 

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