Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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

When trying to remove, got error

"Items collection cannot be modified when the DataSource property is set."


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

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

like image 769
Dhanapal Avatar asked Apr 02 '09 12:04

Dhanapal


People also ask

Which method removes all items from a ListBox?

If we want to clear the items in the Listbox widget, we can use the delete(0, END) method. Besides deleting all the items in the Listbox, we can delete a single item as well by selecting an item from the Listbox, i.e., by using currselection() method to select an item and delete it using the delete() function.

How to remove an item from ListBox in c#?

To remove an itemCall the Remove or RemoveAt method to delete items. Remove has one argument that specifies the item to remove. RemoveAt removes the item with the specified index number.

How do you check ListBox is selected or not?

To determine the items that are selected, you can use the Selected property of the list box. The Selected property of a list box is an array of values where each value is either True (if the item is selected) or False (if the item is not selected).


2 Answers

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.

like image 161
Chris Doggett Avatar answered Nov 14 '22 21:11

Chris Doggett


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.

like image 35
Zack Avatar answered Nov 14 '22 23:11

Zack