Copy one listbox to another

T

tshad

I am trying to copy all the entries from one listbox to another.

I don't want to loop through the listbox to move each one at a time.

I thought there was a way move the data directly from one to the other but
can't remember how it was done.

Is there a way to do it using the DataSource?

Thanks,

Tom
 
P

Peter Duniho

tshad said:
I am trying to copy all the entries from one listbox to another.

I don't want to loop through the listbox to move each one at a time.

I thought there was a way move the data directly from one to the other but
can't remember how it was done.

Is there a way to do it using the DataSource?

Well, if you simply use the same DataSource for both ListBox instances,
then of course they will have the same list elements.

For copying data, there is always the
ListBox.ObjectCollection.AddRange() method. For example:

listBox2.Items.AddRange(listBox1.Items);

Pete
 
T

tshad

Peter Duniho said:
Well, if you simply use the same DataSource for both ListBox instances,
then of course they will have the same list elements.

I don't want to use the same DataSource.

I want to copy the records from one control (which is actually a dropdown
list) to another control (which is a listbox)
For copying data, there is always the ListBox.ObjectCollection.AddRange()
method. For example:

listBox2.Items.AddRange(listBox1.Items);

I tried that

lbJobs.Items.AddRange(ddlJob.Items);

but got an error saying there were some invalid elements.

Could it be because I am copying from a dropdown to a listbox?

Thanks,

Tom
 
P

Peter Duniho

tshad said:
[...]
Could it be because I am copying from a dropdown to a listbox?

Um. Your original question said "I am trying to copy all the entires
from one listbox to another".

If you wanted an answer for a different question, it probably would've
been better to ask that different question instead.

I don't even know how you got a call to
ListBox.ObjectCollection.AddRange() to compile when passing in a
ComboBox.ObjectCollection instance. But yes, I would not expect that to
work. However, if you use the Enumerable.ToArray() method to convert
the ComboBox.ObjectCollection instance to the appropriate type, it should:

lbJobs.Items.AddRange(ddlJob.Items.Cast<object>.ToArray());

You need the Cast() method, because ComboBox.ObjectCollection implements
only IEnumerable, not IEnumerable<object>.

Pete
 
F

Family Tree Mike

I don't want to use the same DataSource.


It looks like _you_ suggested this by asking "Is there a way to do it
using the DataSource?".

I want to copy the records from one control (which is actually a dropdown
list) to another control (which is a listbox)


I tried that

lbJobs.Items.AddRange(ddlJob.Items);

but got an error saying there were some invalid elements.

Could it be because I am copying from a dropdown to a listbox?

Thanks,

Tom

DropDownList, as in Asp.Net? Also your subject said Listbox to another,
as did your question.

So if you want to copy from a dropdownlist to a listbox in asp.net, then
you would need to do so in a loop.

foreach (ListItem li in ddlJob.Items)
lbJobs.Items.Add(li.Text);
 
T

tshad

Peter Duniho said:
tshad said:
[...]
Could it be because I am copying from a dropdown to a listbox?

Um. Your original question said "I am trying to copy all the entires from
one listbox to another".
Actually, I want to do both. I thought that since they are both
ListControls, I could copy the data straight across whether they were the
same controls or not.
If you wanted an answer for a different question, it probably would've
been better to ask that different question instead.

I don't even know how you got a call to
ListBox.ObjectCollection.AddRange() to compile when passing in a
ComboBox.ObjectCollection instance. But yes, I would not expect that to
work. However, if you use the Enumerable.ToArray() method to convert the
ComboBox.ObjectCollection instance to the appropriate type, it should:

lbJobs.Items.AddRange(ddlJob.Items.Cast<object>.ToArray());

You need the Cast() method, because ComboBox.ObjectCollection implements
only IEnumerable, not IEnumerable<object>.

This works great:

lbJobs.Items.AddRange(ddlJob.Items.Cast<ListItem>().ToArray());

Thanks.
 
T

tshad

Family Tree Mike said:
It looks like _you_ suggested this by asking "Is there a way to do it
using the DataSource?".



DropDownList, as in Asp.Net? Also your subject said Listbox to another,
as did your question.

So if you want to copy from a dropdownlist to a listbox in asp.net, then
you would need to do so in a loop.

foreach (ListItem li in ddlJob.Items)
lbJobs.Items.Add(li.Text);

Or as Peter suggested:

lbJobs.Items.AddRange(ddlJob.Items.Cast<ListItem>().ToArray());

I did do it your way first then I saw Peters way. Both work fine.

I assume it's pretty much 6 of one ...

Thanks,

Tom
 
T

tshad

One other question on this using your line of code:

lbJobs.Items.AddRange(ddlJob.Items.Cast<ListItem>().ToArray());

I may have a line in the 0 Item that is "--All--" that I don't want to use.

Is there a way to use Lamda or a delegate to pass all the ListItems except
that one using this line?

Thanks,

Tom
 
P

Peter Duniho

tshad said:
Actually, I want to do both. I thought that since they are both
ListControls, I could copy the data straight across whether they were the
same controls or not.

Taking advantage of a common base class only works when the class
member(s) you're using are in the base class. That's not the case here.
 
P

Peter Duniho

tshad said:
One other question on this using your line of code:

lbJobs.Items.AddRange(ddlJob.Items.Cast<ListItem>().ToArray());

I may have a line in the 0 Item that is "--All--" that I don't want to use.

Is there a way to use Lamda or a delegate to pass all the ListItems except
that one using this line?

See Enumerable.Where() for general purpose filtering of a collection.

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.where.aspx

Alternatively, if you can really know for certain that item is always
the first in the collection, you could instead use the Enumerable.Skip()
method, passing 1 as the count (so you skip the very first element).

http://msdn.microsoft.com/en-us/library/bb358985.aspx

Pete
 
T

tshad

Peter Duniho said:
See Enumerable.Where() for general purpose filtering of a collection.

How would that look with my statement, something like:

lbJobs.Items.AddRange(ddlJob.Items.Cast<ListItem>().ToArray().Where(ddlJob.Items
=> ???);

Thanks,

Tom
 
P

Peter Duniho

tshad said:
How would that look with my statement, something like:

lbJobs.Items.AddRange(ddlJob.Items.Cast<ListItem>().ToArray().Where(ddlJob.Items
=> ???);

I'd use Skip() if you can.

But if you really want to use Where(), or are forced into it:

ddlJob.Items.Cast<ListItem>().Where(item => item.ToString() !=
"--All--").ToArray()
 
T

tshad

Peter Duniho said:
I'd use Skip() if you can.

But if you really want to use Where(), or are forced into it:

ddlJob.Items.Cast<ListItem>().Where(item => item.ToString() !=
"--All--").ToArray()

That works well - as you say skip is probably better.

I can you combine the searches to 2 filters?

For example, the above one is:

lbClients.Items.AddRange(ddlClient.Items.Cast<ListItem>().Where(value
=> value.ToString().Trim() != "--All--").ToArray());

Could you also select all the fields from ddlClients except for the
"--All--" as well as the one selected in ddlClients (if there is one
selected)?

Thanks,

Tom
 
P

Peter Duniho

tshad said:
[...]
Could you also select all the fields from ddlClients except for the
"--All--" as well as the one selected in ddlClients (if there is one
selected)?

I'm not sure why you would want to copy all _except_ a selected item,
but yes…if you can write a C# expression to represent whatever condition
is true if and only if an item is to be copied, then you can just use
that expression as the result of the lambda expression passed to the
Where() method, and it will filter the items accordingly.

Pete
 
T

tshad

Peter Duniho said:
tshad said:
[...]
Could you also select all the fields from ddlClients except for the
"--All--" as well as the one selected in ddlClients (if there is one
selected)?

I'm not sure why you would want to copy all _except_ a selected item, but
yes…if you can write a C# expression to represent whatever condition is
true if and only if an item is to be copied, then you can just use that
expression as the result of the lambda expression passed to the Where()
method, and it will filter the items accordingly.
Not sure how you would do that ( haven't used Lamda expressions much).

But they reason why is because I need to give the user the option of
choosing other items beside the one already selected and this choice is in
another list box that allows multiple selections (which is why I am copying
from a dropdown to a listbox).

So the use makes a selection in the dropdown and the grid is filled and in
another listbox they are given the option to make other selections (that is
identical to the dropdown without the selected item) and the new set of
selections will be added to the grid.

Thanks,

Tom
 
R

Raju S

I achieved with two List box. Check this

http://dot-net-forum.blogspot.com/



tshad wrote:

Not sure how you would do that ( have not used Lamda expressions much).
14-Jan-10

Not sure how you would do that ( have not used Lamda expressions much).

But they reason why is because I need to give the user the option of
choosing other items beside the one already selected and this choice is in
another list box that allows multiple selections (which is why I am copying
from a dropdown to a listbox).

So the use makes a selection in the dropdown and the grid is filled and in
another listbox they are given the option to make other selections (that is
identical to the dropdown without the selected item) and the new set of
selections will be added to the grid.

Thanks,

Tom

Previous Posts In This Thread:

Copy one listbox to another
I am trying to copy all the entries from one listbox to another.

I do not want to loop through the listbox to move each one at a time.

I thought there was a way move the data directly from one to the other but
cannot remember how it was done.

Is there a way to do it using the DataSource?

Thanks,

Tom

tshad wrote:Well, if you simply use the same DataSource for both ListBox
tshad wrote:

Well, if you simply use the same DataSource for both ListBox instances,
then of course they will have the same list elements.

For copying data, there is always the
ListBox.ObjectCollection.AddRange() method. For example:

listBox2.Items.AddRange(listBox1.Items);

Pete

I do not want to use the same DataSource.
I do not want to use the same DataSource.

I want to copy the records from one control (which is actually a dropdown
list) to another control (which is a listbox)


I tried that

lbJobs.Items.AddRange(ddlJob.Items);

but got an error saying there were some invalid elements.

Could it be because I am copying from a dropdown to a listbox?

Thanks,

Tom

tshad wrote:Um.
tshad wrote:

Um. Your original question said "I am trying to copy all the entires
from one listbox to another".

If you wanted an answer for a different question, it probably would have
been better to ask that different question instead.

I do not even know how you got a call to
ListBox.ObjectCollection.AddRange() to compile when passing in a
ComboBox.ObjectCollection instance. But yes, I would not expect that to
work. However, if you use the Enumerable.ToArray() method to convert
the ComboBox.ObjectCollection instance to the appropriate type, it should:

lbJobs.Items.AddRange(ddlJob.Items.Cast<object>.ToArray());

You need the Cast() method, because ComboBox.ObjectCollection implements
only IEnumerable, not IEnumerable<object>.

Pete

On 1/13/2010 2:10 PM, tshad wrote:It looks like _you_ suggested this by asking
On 1/13/2010 2:10 PM, tshad wrote:


It looks like _you_ suggested this by asking "Is there a way to do it
using the DataSource?".



DropDownList, as in Asp.Net? Also your subject said Listbox to another,
as did your question.

So if you want to copy from a dropdownlist to a listbox in asp.net, then
you would need to do so in a loop.

foreach (ListItem li in ddlJob.Items)
lbJobs.Items.Add(li.Text);

--
Mike

Actually, I want to do both.
Actually, I want to do both. I thought that since they are both
ListControls, I could copy the data straight across whether they were the
same controls or not.


This works great:

lbJobs.Items.AddRange(ddlJob.Items.Cast<ListItem>().ToArray());

Thanks.

Or as Peter suggested:lbJobs.Items.AddRange(ddlJob.Items.Cast<ListItem>().
Or as Peter suggested:

lbJobs.Items.AddRange(ddlJob.Items.Cast<ListItem>().ToArray());

I did do it your way first then I saw Peters way. Both work fine.

I assume it is pretty much 6 of one ...

Thanks,

Tom

One other question on this using your line of code:lbJobs.Items.
One other question on this using your line of code:

lbJobs.Items.AddRange(ddlJob.Items.Cast<ListItem>().ToArray());

I may have a line in the 0 Item that is "--All--" that I do not want to use.

Is there a way to use Lamda or a delegate to pass all the ListItems except
that one using this line?

Thanks,

Tom

tshad wrote:Taking advantage of a common base class only works when the
tshad wrote:

Taking advantage of a common base class only works when the class
member(s) you are using are in the base class. That's not the case here.

tshad wrote:See Enumerable.
tshad wrote:

See Enumerable.Where() for general purpose filtering of a collection.

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.where.aspx

Alternatively, if you can really know for certain that item is always
the first in the collection, you could instead use the Enumerable.Skip()
method, passing 1 as the count (so you skip the very first element).

http://msdn.microsoft.com/en-us/library/bb358985.aspx

Pete

How would that look with my statement, something like:lbJobs.Items.
How would that look with my statement, something like:

lbJobs.Items.AddRange(ddlJob.Items.Cast<ListItem>().ToArray().Where(ddlJob.Items
=> ???);

Thanks,

Tom

tshad wrote:I'd use Skip() if you can.
tshad wrote:

I'd use Skip() if you can.

But if you really want to use Where(), or are forced into it:

ddlJob.Items.Cast<ListItem>().Where(item => item.ToString() !=
"--All--").ToArray()

That works well - as you say skip is probably better.
That works well - as you say skip is probably better.

I can you combine the searches to 2 filters?

For example, the above one is:

lbClients.Items.AddRange(ddlClient.Items.Cast<ListItem>().Where(value
=> value.ToString().Trim() != "--All--").ToArray());

Could you also select all the fields from ddlClients except for the
"--All--" as well as the one selected in ddlClients (if there is one
selected)?

Thanks,

Tom

tshad wrote:I am not sure why you would want to copy all _except_ a selected
tshad wrote:

I am not sure why you would want to copy all _except_ a selected item,
but yes?if you can write a C# expression to represent whatever condition
is true if and only if an item is to be copied, then you can just use
that expression as the result of the lambda expression passed to the
Where() method, and it will filter the items accordingly.

Pete

Not sure how you would do that ( have not used Lamda expressions much).
Not sure how you would do that ( have not used Lamda expressions much).

But they reason why is because I need to give the user the option of
choosing other items beside the one already selected and this choice is in
another list box that allows multiple selections (which is why I am copying
from a dropdown to a listbox).

So the use makes a selection in the dropdown and the grid is filled and in
another listbox they are given the option to make other selections (that is
identical to the dropdown without the selected item) and the new set of
selections will be added to the grid.

Thanks,

Tom


Submitted via EggHeadCafe - Software Developer Portal of Choice
Sending SMTP email from within BizTalk Orchestration
http://www.eggheadcafe.com/tutorial...f-1716445b26bc/sending-smtp-email-from-w.aspx
 

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