5 Replies - 43137 Views - Last Post: 24 December 2008 - 01:38 AM Rate Topic: -----

#1 Vintage_hegoog  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 18-December 08

Retrieving string value from listbox object?

Post icon  Posted 24 December 2008 - 12:14 AM

private void bMainsAdd_Click(object sender, EventArgs e)
		{
			index = mainsList.SelectedIndex;

			item = mainsList.Items[index];//retrieve object at selected index into item
			addToOrderTemp = item.ToString();//represent item as string into addToOrderTemp
			
			orderList.Items.Add(addToOrderTemp);
			
			priceTemp = mainPrice.Text;
			
			//adds selected item from sides list to order listbox
			//creates temporary string value of price for calculateSubTotal
			calculateSubTotal();
			discountAmount();
		} 


I am still working on the same project so far, and hit a bitch of a problem (as far as nooby old me is concerned).

My listBox, mainsList is bound to my database dataset, so it contains a collection of DataRowView objects.
I used item as an object to retrieve the data at selected index, and tried to represent it as a string (above), so it could then be added to a seperate listbox, orderList.

But instead of "chicken chow mein" or whatever showing up, it comes up with System.DataRowView when added to orderList.

I have two questions.
Why does that happen?
How do I retrieve the string value from the selected item in mainsList?

Is This A Good Question/Topic? 0
  • +

Replies To: Retrieving string value from listbox object?

#2 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Retrieving string value from listbox object?

Posted 24 December 2008 - 12:35 AM

This is what I would do to add an item to a ListBox control from another ListBox control:

	   // The variable that will keep the index of the selected item
	   int index;

	   // Get the index of the selected item
	   index = listBox1.SelectedIndex;

	   // Add the selected item to the second list box
	   listBox2.Items.Add(listBox1.Items[index].ToString());



The problem you get (displaying System.DataRowView instead of the string value of the item) is because item is an object, and converting it to string will obviously display the object type, not the value of one of the properties of the object.

This post has been edited by Core: 24 December 2008 - 12:36 AM

Was This Post Helpful? 0
  • +
  • -

#3 Vintage_hegoog  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 18-December 08

Re: Retrieving string value from listbox object?

Posted 24 December 2008 - 01:01 AM

Your method was my first attempt. Same thing happens.
Was This Post Helpful? 0
  • +
  • -

#4 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Retrieving string value from listbox object?

Posted 24 December 2008 - 01:03 AM

Well I checked my method and it works perfectly with two ListBox controls.
Was This Post Helpful? 0
  • +
  • -

#5 Vintage_hegoog  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 18-December 08

Re: Retrieving string value from listbox object?

Posted 24 December 2008 - 01:10 AM

View PostCore, on 24 Dec, 2008 - 12:03 AM, said:

Well I checked my method and it works perfectly with two ListBox controls.


It would do, if they are both unbound listboxes, they should only contain collections of objects of type string.
I can't work out how to capture the string value of the DataRowView object.

But, and I'm totally willing to accept it, if I'm too tired and being just dense.. type a slap at me for my ignorance.

I could totally cop out and use comboboxes, but it just ain't as pretty.

This post has been edited by Vintage_hegoog: 24 December 2008 - 01:18 AM

Was This Post Helpful? 0
  • +
  • -

#6 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Retrieving string value from listbox object?

Posted 24 December 2008 - 01:38 AM

Try retrieving the item text by testing this code:

string testString;

testString = listBox1.GetItemText(item);



Check if it gets the string.

This post has been edited by Core: 24 December 2008 - 01:40 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1