Add item update listbox - c#

So I have a databound listbox bound to a list produced from my entity:
myListbox.Datasource = myEntity.ToList();
That works fine. My question is, what is the 'correct' way to add a new element to the entity and have it reflected in my listbox?
Currently, I do this:
myEntity.Add(newItem);
myListbox.Datasource = myEntity.ToList();
Surely there is a better way than resetting the datasource each time?

try this, DataBind() will binds a data source to the invoked server control and all its child controls.
myListbox.DataBind()

Please try this:
myListbox.ResetBindings();
See https://msdn.microsoft.com/en-us/library/system.windows.forms.control.resetbindings%28v=vs.110%29.aspx

Related

Grid data setup control

I am struggling with the grid view control and it seems I miss something as I am
not able to show data in it. I am trying the bind the control to a list of some
objects.
Can you please walk me through and explain how I need to setup the control?
Assuming a lot here since I don't have any code to go off of....
Given a List<GridDataModel> you would set your DataSource property to it and call DataBind method of your GridView.
var myData = GetMyData(); //this function builds the List<GridDataModel>
MyGrid.DataSource = myData;
MyGrid.DataBind();
A more complete example.

ComboBox Items Disappearing while Re binding

In my application i have a combo-box with list of some data from DB. The combo-box data-source is given set to a binding source. The binding source has a data-set as Data-source and a Data-table as data-member.
I update this combo-box after adding or editing or deleting an entry. The updating is done by again calling the code for binding. Now all the items in the combo-box disappears.
I don't know how it happens.
A sample work is in the link http://www.filedropper.com/students_1. the db is included with it.
In your code in 'void GetData()' write the code
bs = new BindingSource(new System.ComponentModel.Container());
this has to be executed every time whenever you want to load new datasource
I don't have your code so I can't give the exact solution but you can try with the following tip:
If you are calling your bind code inside if(!Page.IsPostback) block then remove from there and call on page load.

Why isn't my datagrid updating?

I have an object that has as one of its properties, a List. I want to bind a datagrid to that list, such that when I add objects to the grid, the datagrid updates. I tried:
myDataGrid.DataSource = myObject.MyList;
but when I update the datasource with new rows, the grid doesn't update.
Then I tried:
myDataGrid.DataSource = null;
myDataGrid.DataSource = myObject.MyList;
Calling the above code every time I added an item. This resulted in an error when clicking on the grid (specifically, index -1 has no data, something to do with the datagridview.get_current internally. Happens despite the fact that I'm not clicking the -1st row).
So then I tried:
myDataGrid.DataBindings.Add(new Binding("DataSoruce",myObject,"MyList",false,DataSourceUpdateMode.OnPropertyChanged));
That didn't reflect the updates either, so I added:
myDataGrid.DataBindings[0].ReadValue();
whenever I added an item, but it has no effect either. I feel like I'm circling around a simpler solution to this problem, but I can't seem to find it. Any pro tips?
You seem to already know this, but you want to use a BindingList if at all possible here. Any hamfisted attempt to make a List function like a BindingList is just going to be a lot more pain than simply copying the elements from a List you already have to a BindingList.
If I changed my type from List to BindingList, all the problems go away and the grid autoupdates as expected.

How do I prevent selectedValue altering when calling tableAdapter's Fill() method?

I have bound my ListBox to some data.
The problem is when I call myTableAdapter.Fill(..) method, SelectedValue changes to whatever is the first item ID in the list. Although "Selected Value" in VS is not bound anywhere (see image).
alt text http://img370.imageshack.us/img370/2548/ss20090108212745qz2.png
How do I prevent this behaviour, please?
Thank you very much for helping.
You shouldn't be binding on each request. If you absolutely have to bind on each request for some reason you have to set SelectedIndex on the ListBox manually. This is because the Fill method first clears the list then creates new list items for the fetched data.
The simplest way I can think of is to change your table adapter fill code to something like this:
string preSelected = myDropDownList.SelectedValue;
myTableAdapter.Fill(myDataTable);
myDropDownList.SelectedValue = preSelected;
You will run into an issue if the item doesn't exist anymore, so you may want to add a condition to check for that.

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
"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".
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.
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.
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
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.
This worked for me
DataTable temp = (DataTable)lstBlocks.DataSource;
temp.Rows.RemoveAt(position);
its vary simple , assign a new blank value to listbox
eg..
Dim ABC As New List(Of String)()
ListBox1.DataSource = ABC
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;

Categories