WPF ListBox Question

G

Gordon Padwick

I am creating a WPF project (using Visual Studio 2008 Professional) that
gets data from an Access database table and displays that data in a ListBox
control. The database table contains three text columns and one memo column.
The ListBox uses a DataTemplate to display the four columns of data (as
explained in Microsoft's "How to: Bind to an ADO.NET Data Source).

At this point, I want to select a row in the ListBox and display the content
of the selected row in four separate text boxes so that they can be edited
and used in other parts of the project.

My window contains a button that, when clicked, calls a temporary event
handler that contains:
String curItem = myList.SelectedItem.ToString();
MessageBox.Show(curItem);

The message box of course displays "System.Data.DataRowView". I suspect I
need to override the ToString method to see the actual contents of the
selected ListBox item, but I don't understand how to create such an
override.

Visual Studio Help and the various books about C# and WPF that I've looked
at provide a lot of information about populating a ListBox control, but very
little about working with data in the control.

Can someone provide some hints about extracting data from a selected item in
a ListBox control? Thanks.
 
P

Peter Duniho

I am creating a WPF project (using Visual Studio 2008 Professional) that
gets data from an Access database table and displays that data in a
ListBox control. The database table contains three text columns and one
memo column. The ListBox uses a DataTemplate to display the four columns
of data (as explained in Microsoft's "How to: Bind to an ADO.NET Data
Source).

At this point, I want to select a row in the ListBox and display the
content of the selected row in four separate text boxes so that they can
be edited and used in other parts of the project.

My window contains a button that, when clicked, calls a temporary event
handler that contains:
String curItem = myList.SelectedItem.ToString();
MessageBox.Show(curItem);

The message box of course displays "System.Data.DataRowView". I suspect
I need to override the ToString method to see the actual contents of the
selected ListBox item, but I don't understand how to create such an
override.

Perhaps you could explain why you care?

Since you want to populate four different text boxes, surely simply
getting a single string from the ListBox row isn't really going to be all
that useful in the long run. As the experiment you've done shows, the
SelectedItem is in fact a DataRowView object, from which you can extract
individual column values, just as you would from a DataRow directly.
Visual Studio Help and the various books about C# and WPF that I've
looked at provide a lot of information about populating a ListBox
control, but very little about working with data in the control.

Can someone provide some hints about extracting data from a selected
item in a ListBox control? Thanks.

Assuming you have columns "text1", "text2", "text3", and "memo1", seems
like something like this would work fine:

DataRowView view = (DataRowView)myList.SelectedItem;

textBox1.Text = view["text1"].ToString();
textBox2.Text = view["text2"].ToString();
textBox3.Text = view["text3"].ToString();
textBox4.Text = view["memo1"].ToString();

If that's not helpful, I think you'll need to be more specific about what
exactly you're trying to do and what's not working.

Pete
 
R

Registered User

I am creating a WPF project (using Visual Studio 2008 Professional) that
gets data from an Access database table and displays that data in a ListBox
control. The database table contains three text columns and one memo column.
The ListBox uses a DataTemplate to display the four columns of data (as
explained in Microsoft's "How to: Bind to an ADO.NET Data Source).

At this point, I want to select a row in the ListBox and display the content
of the selected row in four separate text boxes so that they can be edited
and used in other parts of the project.

My window contains a button that, when clicked, calls a temporary event
handler that contains:
String curItem = myList.SelectedItem.ToString();
MessageBox.Show(curItem);

The message box of course displays "System.Data.DataRowView". I suspect I
need to override the ToString method to see the actual contents of the
selected ListBox item, but I don't understand how to create such an
override.
No you don't. Go to MSDN and examine the members of type
System.Data.DataRowView. To get the second column in the selected row
you might use
MessageBox.Show(curItem.Row[1].ToString());
or
MessageBox.Show(curItem.Item[1].ToString());

Visual Studio Help and the various books about C# and WPF that I've looked
at provide a lot of information about populating a ListBox control, but very
little about working with data in the control.

Can someone provide some hints about extracting data from a selected item in
a ListBox control? Thanks.
Each row is represented by some type of object. If the control's data
source is of type List<T> the selected item can be cast as type T.

Foobar fb = myList.SelectedItem as Foobar;
or
Foobar fb = (Foobar)myList.SelectedItem;

regards
A.G.
 

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