Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use ListBox in my project. When I get data for update then same data show in listbox1 and listbox2. But I try to remove data from listbox1 which is alread in Listbox2

What I have tried:

if (updateById == null) return;
btnSave.CommandArgument = updateById.CityDetailsId.ToString(CultureInfo.InvariantCulture);
BindStateDetails();
ddlState.SelectedValue = Convert.ToString(updateById.StateId);
BindCityList();
//libAdminCity.DataSource = updateById.CityName;
message3 = updateById.VCityId;
message4 = updateById.CityName;
string[] arr = message4.Split(',');

for (int i = 0; i < arr.Length; i++)
{
    foreach (ListItem item in libAdminCity.Items)
    {
       // int j = 0;
       // if (item.Text == arr[i])
        if (libVendorCity.Items.Contains(item))
        {

            //libVendorCity.Items.Add(new ListItem(Convert.ToString(item.Text), Convert.ToString(item.Value)));
            libAdminCity.Items.Remove(item);

        }
        //else
        //{
        //    libVendorCity.Items.Add(new ListItem(Convert.ToString(item.Text), Convert.ToString(item.Value)));
        //}

    }


}
Posted
Updated 10-Oct-17 13:17pm
Comments
Atlapure Ambrish 10-Oct-17 23:08pm    
How you are binding data to your list boxes?

What problem/error you are getting while removing the item from list?
Karthik_Mahalingam 10-Oct-17 23:45pm    
Place a break point and check whether it goes inside the condition.

1 solution

Your problem seems to be that your Contains() never returns true as it does not exactly what you want. Two items that have the same Text are still two distinct items with regards to this method.

You need to loop over the libVendorCity ListBox and compare each item Text property and if it matches you do the Remove().
 
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