3 Replies - 1482 Views - Last Post: 16 October 2009 - 12:08 PM Rate Topic: -----

#1 mondo3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-October 09

How do I Retrieve a value from a combobox?

Posted 14 October 2009 - 10:48 AM

As shown below, I've filled a customer combobox with a customer id (named intItem) and customer name (named strItem).
What would be the syntax to get the intItem after the user makes a selection?
			while (rsCustomer.Read())
			{
				// Load Customer Names into combobox.
				intItem = Convert.ToInt32(rsCustomer["CustomerID"].ToString());
				strItem = rsCustomer["CustomerName"].ToString();
				Customer item = new Customer(intItem, strItem);
				cboCustomerName.Items.Add(strItem);
			}




Is This A Good Question/Topic? 0
  • +

Replies To: How do I Retrieve a value from a combobox?

#2 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: How do I Retrieve a value from a combobox?

Posted 14 October 2009 - 11:45 AM

it's actually very simple.

string customerName = cboCustomerName.Text;


Was This Post Helpful? 0
  • +
  • -

#3 mondo3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-October 09

Re: How do I Retrieve a value from a combobox?

Posted 14 October 2009 - 01:04 PM

ok...i see that the last line of code should have been:

cboCustomerName.Items.Add(Item);


so that this works:

int intCustomerID=((Customer)cboCustomerName.SelectedItem).CustomerID;

Thanks for trying!

View Postmondo3, on 14 Oct, 2009 - 09:48 AM, said:

As shown below, I've filled a customer combobox with a customer id (named intItem) and customer name (named strItem).
What would be the syntax to get the intItem after the user makes a selection?
			while (rsCustomer.Read())
			{
				// Load Customer Names into combobox.
				intItem = Convert.ToInt32(rsCustomer["CustomerID"].ToString());
				strItem = rsCustomer["CustomerName"].ToString();
				Customer item = new Customer(intItem, strItem);
				cboCustomerName.Items.Add(strItem);
			}



This post has been edited by mondo3: 14 October 2009 - 01:08 PM

Was This Post Helpful? 0
  • +
  • -

#4 suprgoogie   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-October 09

Re: How do I Retrieve a value from a combobox?

Posted 16 October 2009 - 12:08 PM

After you have added all the items do this :


cboCustomerName.ValueMember="intItem"
cboCustomerName.DisplayMember="strItem"

that way your specifying that it's showing the strItem on the ComboBox, but that the intItem is what u need to get from the selection, to retrieve the value do it on the event : SelectedIndexChanged of the cboCustomerName

and it is very simple, just get the SelectedValue property of the ComboBox, like this

cboCustomerName.SelectedValue

u may have to do casting if u want the value as an Integer,String or whatever, so if u want to get the value as string just do

string text = cboCustomerName.SelectedValue.ToString();

that will get u the value selected by the user!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1