Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How to exchange the items from one list box to other when both list box are binded to the data source?

C#
private void buttonSelectAllAvailableStops_Click(object sender, EventArgs e)
{
   // this.stopBindingSource.DataSource = null;
   // this.routeBindingSource1.DataSource = null;

    foreach (string item in listBoxAvailableStops.Items)
    {
        if (listBoxSelectedStops.Items.Contains(item) == false)
        {
            listBoxSelectedStops.Items.Add(item);
        }
        else
        {
            MessageBox.Show("These Stops are already selected!");
        }
    }
}


if use above code it will show error when binding the list box we cant add the item to the list box.

[edit]OP response identified code fragment; removed rest of code dump for legibility - OriginalGriff[/edit]
Posted
Updated 9-Mar-11 20:06pm
v2
Comments
m@dhu 10-Mar-11 1:45am    
No one will be interested in seeing your entire code please be specific with the code you are having the problem.
sairam.bhat 10-Mar-11 1:54am    
k sir private void buttonSelectAllAvailableStops_Click(object sender, EventArgs e)
{


// this.stopBindingSource.DataSource = null;
// this.routeBindingSource1.DataSource = null;

foreach (string item in listBoxAvailableStops.Items)
{
if (listBoxSelectedStops.Items.Contains(item) == false)
{
listBoxSelectedStops.Items.Add(item);
}
else
{
MessageBox.Show("These Stops are already selected!");
}
}
}
sairam.bhat 10-Mar-11 1:56am    
if use above code it will show error when binding the list box we cant add the item to the list box.
Sandeep Mewara 10-Mar-11 1:47am    
1. Why full code? You should only paste relevant code. No one will look at such long codebase.
2. Why code? Whats the issue/error?

C#
bool found = false;
foreach (DataRowView drv1 in listBox1.Items)
{
    foreach (DataRowView drv2 in listBox2.Items)
    {
        if (drv1.Row.ItemArray[0].ToString().Equals(drv2.Row.ItemArray[0].ToString()))
        {
        found = true;
        break;
        }
    }
    if (!found)
    {
        DataTable dt = (DataTable)listBox2.DataSource;
        dt.Rows.Add(drv1.Row.ItemArray);
        listBox2.DataSource = dt;
    }
    found = false;
}
 
Share this answer
 
Comments
sairam.bhat 10-Mar-11 3:16am    
sir i got error in this line - DataTable dt = (DataTable)listBoxSelectedStops.DataSource;
Error- Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'.
Ryan Zahra 10-Mar-11 3:22am    
What is the data source that you're using to bind with the list box?
sairam.bhat 10-Mar-11 3:26am    
MS access
Ryan Zahra 10-Mar-11 3:27am    
Ok, but how are you doing the binding?
sairam.bhat 10-Mar-11 3:33am    
i will set the data source property of the listBox as stopBindingsource and display member as field name.
You are trying to make modification to the collection being iterated within the ForEach block. This gives you error.
You can use a while loop or for loop to iterate through the list items.
Check the sample code
Add the listitems out of the foreach block.
C#
foreach ( string item in listBox1.Items)
            {
                if (listBox2.Items.Contains(item) == false)
                { str += item+"|"; }
                else
                { MessageBox.Show("These Stops are already selected!"); }
            }
            foreach (string s in str.Split('|'))
            {
                listBox1.Items.Add(s);
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900