简体   繁体   中英

How to remove selected items from ListBox when a DataSource is assigned to it in C#?

How to remove selected items from ListBox when a datasource is assigned to it in C#?

When trying to remove, got error


But when i try to remove item from datasource (datatable),

it thorws error as "datarow is not in current row collection".

Find that item in the DataSource object and remove it, then re-bind the ListBox.

EDIT :

Here's how you delete from a DataTable as your DataSource, regardless of the .NET version.

DataRowView rowView = listBox.SelectedItem as DataRowView;

if (null == rowView)
{
    return;
}

dt.Rows.Remove(rowView.Row);

I haven't tried with anything other than WinForms DataGridViews, but I highly recommend BindingListView , which is both faster than DataTables/Views and allows you to bind generic List<T>s as your DataSource.

Alternatively, use a list that implements IBindingList or inherits from BindingList. When objects are added or removed from a Binding List, any controls bound to it are automatically notified of the change and will update themselves accordingly. If you are using BindingList and your class also implements INotifyProperty changed, Any changes to class properties will also be updated automatically in the databinding control. For example, if a column in a datagrid(view) is bound to a property, "Name", and you change "Name" in the datasource, the datagrid will automatically update. If you add a new item to the datasource, the datagrid will update automatically. Binding List also supports notification in the other direction. If a user edits the "Name" field ina datagrid, the bound object will be updated automatically. Going off topic slightly, if you go a little further and impliment "SupportsSortingCore" and the associated methods in BindingList, you can add automatic sorting to your data. Clicking on a columnm header will automatically sort the list and display the header sort direction arrow.

when you get the message "Items collection cannot be modified when the DataSource property is set." setting the datasource to something else, empty list or null does not help when the code initializecomponent is not completed.

to avoid that error, one must do the change of datasource or the item list during or after form load.

I know it does not seem to make sense. Hoever, the visual studio designer will generate code in the form designer.cs or vb that will add items to the listbox if any code that changes the items is found before end of initialize components

This worked for me

        DataTable temp = (DataTable)lstBlocks.DataSource;
        temp.Rows.RemoveAt(position);

ListBox implementation is bugged, you need to create a new data source instance for the component for it to recognize a change.

Eg:

ActivitiesList.DataSource = _activities;

_activities = new List<Activity>(_activities);
_activities.Remove((Activity)ActivitiesList.SelectedItem);

ActivitiesList.DataSource = _activities;

If the ListBox has a datasource assigned, you must remove items from the datasource and then rebind the ListBox

You need to modify the data source rather than the Items collection of the control. Depending on what kind of data source you are binding to, there are going to be different things you have to do so that your UI updates.

The best way is find a collection that fits your needs and implements IBindingList or IBindingListView. Those two interfaces implement even handlers that listen for a CollectionChanged event and update your UI accordingly.

If your collection doesn't support those interfaces, you're going to have to re-bind your data source every time somebody adds/removes an item.

While Chris Doggett posted a valid solution, I ran into problems while using it. By using that method it was not allowing a subsequent GetChanges(DataRowState.Deleted) to work properly.

To better solve my problem, I only had to change a single line - the last line.

DataRowView rowView = listBox.SelectedItem as DataRowView;

if (null == rowView)
{
    return;
}

rowView.Row.Delete();

This allowed my GetChanges call to work properly.

its vary simple, assign a new blank value to listbox eg..

Dim ABC As New List(Of String)()

ListBox1.DataSource = ABC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM