Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why it doesn't work for me??? 1

Status
Not open for further replies.

rahmanjan

Programmer
Apr 13, 2003
180
AU
Hi all,

I have bound my combobox to database using Dataset. as:

DataSet ds=new DataSet();
ds=data.ReturnAll(sqlstr);
cboCategory.DataSource=ds.Tables[0];
cboCategory.ValueMember="CategoryID";
cboCategory.DisplayMember="Category";

It is filling the combox Okay.

When i am trying to get an item i do as follow:

Object selectedItem = cboCategory.SelectedItem;
MessageBox.Show(selectedItem.ToString());

It returns System.Data.RowView ??? I don't know why? My questions:

1-How can i get the text of the combox?
2-How can i get the index of the item bound in the combox?

regards
 
I saw a code like this in the help, but there the combo box was bounded to strings, so the function ToString knows what to do, but in your case, the system doesn't know how to make this Object a string.
For example:
Code:
namespace ns
{
struct st
{
int a;
}
}
if you call st.ToString() which was inherited from class Object, you will see "ns.st" which is the object type.
solution - override the ToString:
Code:
struct st
{
int a;
string Tostring()
{
return Convert.ToString(a);
}
}
you can inherit the System.Data.RowView class to your defined class abd override ToString.
 
sorry,

I don't have a class or somehting here. I have the code

Object selectedItem = cboCategory.SelectedItem;
MessageBox.Show(selectedItem.ToString());

under the click event of the combobox.

The question here is that the combox shows a list of 'stirngs' and i select one. Why it should be different in VB.net and ASP.net and C#.net???

regards
 
The selected item is an object, not the string displayed. The .ToString is returning correctly as it doesn't know what particular item to return and thus is returning the type. Try casting a textbox to object and seeing what that returns in the ToString method.


You are after the string contained within the bound column within that object. What you need to do is extract it. As you are using a bound datatable, you can use something along the lines of the following

System.Data.Datarow selectedItem = cboCategory.SelectedItem;
MessageBox.Show(selectedItem.Item("NameOfBoundColumn));

Craig

PS Like the irony of you using my post having been told it was VB......apologies accepted on a postcard.




 
Hi craig,

Thanks very much mate. Though ur code raises the error but u deserve the *star*

should have been:

System.Data.Datarow selectedItem = (System.Data.Datarow) cboCategory.SelectedItem;

regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top