Unable to update listbox from database - c#

In my application I have listBox and its items source is ObservableCollection, but when I add data, this listBox won't update. In constructor i use:
listBoxPersons.ItemsSource = UserContacts;
and user UserContacts is static ObservableCollection, which is initialised from database and when I open the programm, it shows correct data. But when I add new item into database and in same time using same code listBoxPersons.ItemsSource = UserContacts;, then listBox won't display added item. But when I logout and login again to my application, it displays my added items.

You literally said add new item into the database. You need to also add the item to the ObservableCollection.

Perhaps the problem is with the static ObservableCollection.
Try it with a non-static collection.

Related

Can't manually add item to listbox that is populated iterating through a list

In a C# WinForms project I'm querying a database to get a list of values to populate a listbox with. The query populates a List and then I iterate through that to add the list items to the listbox.
lsNewValuesList = dbGetNewValueInfo.GetNewValuesDgvData(strNewValuesQuery);
foreach (string strItem in lsNewValuesList)
{
lsBxNewValues.Items.Add(strItem);
}
After that's done, I need to manually add an item to the top of the list, which I'm doing via, lsBoxNewValues.Items.Insert(0, "DELETE");. But when I run it I get the message,
Items collection cannot be modified when the DataSource property is set.
Here is a screenshot of the error (to clarify some questions):
Looking into that I'm understanding that error arises when the listbox is populated with a datasource, but I'm just populating it from a string list. Is that, technically, a datasource then?
How do I accomplish what I'm trying to do?
[UPDATE]
Okay, I've done some fiddling around, albeit without resolving my issue, and I've tried the following:
lsNewValuesList = dbGetNewValueInfo.GetNewValuesDgvData(strNewValuesQuery);
lsNewValuesList.Insert(0, "DELETE");
lsBxNewValues.DataSource = lsNewValuesList;
//foreach (string strItem in lsNewValuesList)
//{
// lsBxNewValues.Items.Add(strItem);
//}
Instead of inserting "DELETE" in the ListBox (which is what I was originally trying and was causing the error), I inserted it at index 0 of the List, the datasource of the ListBox, and then set the ListBox's datasource. "DELETE" is showing up in the ListBox, but it's getting alphabetized along with the rest of the items. I'm not doing any sorting of the list or the ListBox - I'm using ORDER BY in the database query, however. CyberZeus suggested I use Refresh() on the ListBox, but that didn't have any effect.
Yes, a list of strings is a datasource.
You have to add the data retrieved from the database to the datasource of the ListBox. You may need to refresh the ListBox.

Trying to bind make a gridview autoupdate in uwp c#

i want to make a dbgrid update data from a list when i write into it in a uwp. Because you set the datasource in xaml i cant seem to find a way to auto update it? i have tried to use a observablecollection but i cant seem to figure out how it works.
foreach (var item in dataAccess.GetData())
{
cars.Add(item);
}
this is how i read the values into the list and it runs on a timer so it will add data every time the timer ticks and then the data should display in the dbgrid.
List<CarSimulation> cars = new List<CarSimulation>();
that is how i declaired the lsit that i am using to bind the data to the dbgrid and because i am reading the data from a sqlite database first the dbgrid does not display anything. any tips on how i can update it dynamically?
Simply change List into ObservableCollection. ObservableCollection is a collection class that provides notifications when items get added or removed.
ObservableCollection<CarSimulation> cars = new ObservableCollection<CarSimulation>();
Here's an example that's already in the community

How to Add/Remove items to ComboBox Collection?

I'm trying to remove selected item from ComboBox Collection:
Items are added manually, as a Collection, in design time.
buttonClick:
cb01.Items.Remove(cb01.SelectedItem);.
This deletes the item, but next time I open the Form - the item appears again.
Must I have a database for 5-6 items ?
Please help.
This cb01.Items.Remove(cb01.SelectedItem); will only remove from the combobox, not from the datasource binded to the combobox. You may remove it from the datasource and re bind the source.
If you are binding the combobox with an array in your code, then you may save the array on a persistent storage, either a database table, or XML file and upon deletion from combobox you should remove the element from the array and save the changes to the persistent storage
You can also work with Files (existing in System.IO Namespace) if you don't want to use a database server. for 5/6 items it's not worth to use database, and in a file you can easily find the item's line and delete the line.
hope it helps.

BindingSource And Composite Properties

I have a method that I call in a form that returns a List and then I throw that into a BindingSource and throw the BindingSource into a DataGridView. The Order class has a property called Items that contains a List. My Order and Item class both implement the INotifyPropertyChanged Interface.
What I'm trying to do is that when a row is clicked in the DataGridView it pulls up the list of items associated with the order from it's Items property into another grid. I want to ensure that those items are 2 way binded to the grid but I can't call it the way I'd like to...
itemsGrid.DataSource = orderBindingSource[orderGrid.CurrentRow.Index].Items;
I can't access the properties of the list from within the BindingSource (After the . operator, Items is not an available option)... Any recommendations? I've tried using a BindingList<Order> and BindingList<List<Order>> but was getting implicit conversion errors. Any suggestions?
*EDIT: Ok I've managed to get the BindingList to work. I didn't know I had to add the list like this
orderList = new BindingList<Order>(Method To Retrieve List<Order>)
Aside from this, the first grid displaying the orders works and updates perfectly. The sub grid which contains the Order's Items bound like
itemsGrid.Datasource = orderList[ordersGrid.CurrentRow.Index].Items;
It displays properly. I don't have editing enabled as I have a few textboxes that are setup and each textbox is bound to the Item from the orderList of the currently selected row of the ordersGrid. I can update the data in the textboxes for the selected item in the subgrid but the changes when I leave the textbox, the sub grid's row for that item property edited does not update as I leave the textbox. It will, however, update if I select another item in the grid. How can I get it to update as I leave the textbox instead of having to click on another row in the grid?

Add Items to a drop down list whose datasource is already set in a Windows Form Application

I have a drop down list whose4 datasoruce is already set, I need to add an extra item, in webapplications it's easy, using Items.Insert(index, newItem). but in windows applications it is not working, any body can help!
Note: I need to add the items without affecting the datasource at all
thanks
If your DataSource doesn't have an Add method. You will have to either create your own DataSource (List<...>) or add item iteratively.
If the data source is a dataset or list, try adding items to it and those will appear in the drop down.

Categories

Resources