How to iterate through a listbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following code:
sendToAgent.DataSource = someDataSet.Tables[0];
someListBox.DisplayMember = "AGENTNAME";
someListBox.ValueMember = "AGENTID";
someListBox.Tag = "USERNAME";

What I want to do is iterate through the selected values and retrieve the
displaymember, valuemember and Tag.

I saw another post about extracting a DataRowView and using it but it
wouldn't work for me.

I don't understand why this is so arcane.

Any help would be MUCH MUCH appreciated!
 
Hi,

Dilip M said:
I have the following code:
sendToAgent.DataSource = someDataSet.Tables[0];
someListBox.DisplayMember = "AGENTNAME";
someListBox.ValueMember = "AGENTID";
someListBox.Tag = "USERNAME";

First you have "sendToAgent" then "someListBox", is it a typo ? Does the
ListBox have a DataSource?

ListBox.Text will give you the value of the DisplayMember field of selected
row.
ListBox.SelectedValue will give you the value of the ValueMember field of
selected row.
Tag doesn't apply to the items, it only allows you to associate any object
with the ListBox itself.
What I want to do is iterate through the selected values and retrieve the
displaymember, valuemember and Tag.

I saw another post about extracting a DataRowView and using it but it
wouldn't work for me.

Yes, but you don't show how you tried it or what went wrong, because if you
bind to a DataTable and allow multiple selection then you need to get the
values through a DataRowView.

foreach ( DataRowView drv in someListBox.SelectedItems )
{
string agentName = (string) drv[someListBox.DisplayMember];
int agentID = (int) drv[someListBox.ValueMember];
string userName = (string) drv[(string)someListBox.Tag];
// ....
}

hth,
greetings
 
HI Bart,

Actually it was your post that I mentioned earlier. Thanks very much for
the quick reply!

Yes, 'someListBox' and 'sendToAgent' are the same.

I asumed the Tag value would appy to each item as I need a third field for
this. I guess I'll have to look for a workaround.

I will try this out.

Thanks again!

Bart Mermuys said:
Hi,

Dilip M said:
I have the following code:
sendToAgent.DataSource = someDataSet.Tables[0];
someListBox.DisplayMember = "AGENTNAME";
someListBox.ValueMember = "AGENTID";
someListBox.Tag = "USERNAME";

First you have "sendToAgent" then "someListBox", is it a typo ? Does the
ListBox have a DataSource?

ListBox.Text will give you the value of the DisplayMember field of selected
row.
ListBox.SelectedValue will give you the value of the ValueMember field of
selected row.
Tag doesn't apply to the items, it only allows you to associate any object
with the ListBox itself.
What I want to do is iterate through the selected values and retrieve the
displaymember, valuemember and Tag.

I saw another post about extracting a DataRowView and using it but it
wouldn't work for me.

Yes, but you don't show how you tried it or what went wrong, because if you
bind to a DataTable and allow multiple selection then you need to get the
values through a DataRowView.

foreach ( DataRowView drv in someListBox.SelectedItems )
{
string agentName = (string) drv[someListBox.DisplayMember];
int agentID = (int) drv[someListBox.ValueMember];
string userName = (string) drv[(string)someListBox.Tag];
// ....
}

hth,
greetings
I don't understand why this is so arcane.

Any help would be MUCH MUCH appreciated!
 
Hi Dilip,

There is no problem to read the USERNAME or any other field from the ListBox. Simply use the SelectedItem. When you fill a ListBox with data from a DataTable, the objects that are inserted are DataRowViews, one per row in the DataTable.

DataRowView row = (DataRowView)listBox1.SelectedItem;
string UserName = row["USERNAME"];



HI Bart,

Actually it was your post that I mentioned earlier. Thanks very much for
the quick reply!

Yes, 'someListBox' and 'sendToAgent' are the same.

I asumed the Tag value would appy to each item as I need a third field for
this. I guess I'll have to look for a workaround.

I will try this out.

Thanks again!

Bart Mermuys said:
Hi,

Dilip M said:
I have the following code:
sendToAgent.DataSource = someDataSet.Tables[0];
someListBox.DisplayMember = "AGENTNAME";
someListBox.ValueMember = "AGENTID";
someListBox.Tag = "USERNAME";

First you have "sendToAgent" then "someListBox", is it a typo ? Does the
ListBox have a DataSource?

ListBox.Text will give you the value of the DisplayMember field of selected
row.
ListBox.SelectedValue will give you the value of the ValueMember field of
selected row.
Tag doesn't apply to the items, it only allows you to associate any object
with the ListBox itself.
What I want to do is iterate through the selected values and retrieve the
displaymember, valuemember and Tag.

I saw another post about extracting a DataRowView and using it but it
wouldn't work for me.

Yes, but you don't show how you tried it or what went wrong, because if you
bind to a DataTable and allow multiple selection then you need to get the
values through a DataRowView.

foreach ( DataRowView drv in someListBox.SelectedItems )
{
string agentName = (string) drv[someListBox.DisplayMember];
int agentID = (int) drv[someListBox.ValueMember];
string userName = (string) drv[(string)someListBox.Tag];
// ....
}

hth,
greetings
I don't understand why this is so arcane.

Any help would be MUCH MUCH appreciated!
 
Hi Morten,

I am doign a multiselect and trying to loop through the selected values. I
think my biggest confsion was that ListBox.SelectedItems returns a
SelectedObjectCollection.
It is not at all intuitive that it is a bunch of DataRowView items.
Bart's mail earlier cleared that up.

Morten Wennevik said:
Hi Dilip,

There is no problem to read the USERNAME or any other field from the ListBox. Simply use the SelectedItem. When you fill a ListBox with data from a DataTable, the objects that are inserted are DataRowViews, one per row in the DataTable.

DataRowView row = (DataRowView)listBox1.SelectedItem;
string UserName = row["USERNAME"];



HI Bart,

Actually it was your post that I mentioned earlier. Thanks very much for
the quick reply!

Yes, 'someListBox' and 'sendToAgent' are the same.

I asumed the Tag value would appy to each item as I need a third field for
this. I guess I'll have to look for a workaround.

I will try this out.

Thanks again!

Bart Mermuys said:
Hi,

I have the following code:
sendToAgent.DataSource = someDataSet.Tables[0];
someListBox.DisplayMember = "AGENTNAME";
someListBox.ValueMember = "AGENTID";
someListBox.Tag = "USERNAME";

First you have "sendToAgent" then "someListBox", is it a typo ? Does the
ListBox have a DataSource?

ListBox.Text will give you the value of the DisplayMember field of selected
row.
ListBox.SelectedValue will give you the value of the ValueMember field of
selected row.
Tag doesn't apply to the items, it only allows you to associate any object
with the ListBox itself.


What I want to do is iterate through the selected values and retrieve the
displaymember, valuemember and Tag.

I saw another post about extracting a DataRowView and using it but it
wouldn't work for me.

Yes, but you don't show how you tried it or what went wrong, because if you
bind to a DataTable and allow multiple selection then you need to get the
values through a DataRowView.

foreach ( DataRowView drv in someListBox.SelectedItems )
{
string agentName = (string) drv[someListBox.DisplayMember];
int agentID = (int) drv[someListBox.ValueMember];
string userName = (string) drv[(string)someListBox.Tag];
// ....
}

hth,
greetings


I don't understand why this is so arcane.

Any help would be MUCH MUCH appreciated!
 
Thank you for this solution. I wasted so much time searching Microsoft's
documentation. I should have come here first. How hard would it have been
to include a "How to iterate selected items in a ListBox" topic in help?

WR

Bart Mermuys said:
Hi,

Dilip M said:
I have the following code:
sendToAgent.DataSource = someDataSet.Tables[0];
someListBox.DisplayMember = "AGENTNAME";
someListBox.ValueMember = "AGENTID";
someListBox.Tag = "USERNAME";

First you have "sendToAgent" then "someListBox", is it a typo ? Does the
ListBox have a DataSource?

ListBox.Text will give you the value of the DisplayMember field of selected
row.
ListBox.SelectedValue will give you the value of the ValueMember field of
selected row.
Tag doesn't apply to the items, it only allows you to associate any object
with the ListBox itself.
What I want to do is iterate through the selected values and retrieve the
displaymember, valuemember and Tag.

I saw another post about extracting a DataRowView and using it but it
wouldn't work for me.

Yes, but you don't show how you tried it or what went wrong, because if you
bind to a DataTable and allow multiple selection then you need to get the
values through a DataRowView.

foreach ( DataRowView drv in someListBox.SelectedItems )
{
string agentName = (string) drv[someListBox.DisplayMember];
int agentID = (int) drv[someListBox.ValueMember];
string userName = (string) drv[(string)someListBox.Tag];
// ....
}

hth,
greetings
I don't understand why this is so arcane.

Any help would be MUCH MUCH appreciated!
 
Back
Top