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.
Related
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.
The code I am working on uses a WPF DataGrid to store a table of entries, bound to an ObservableCollection of items, with the possibility for the user to create new items using the blank row at the bottom of the table.
When the user selects a cell in the new row, right as they type their first keystroke, the DataGrid creates a new blank item and inserts it in the ObservableCollection. Once the user hits Enter or takes focus away from the cell, the new text is committed as an edit to the item.
Is there any way to change the behavior of the DataGrid so that it does not create a blank item before the user is done typing? Or, at least, does not add the blank item to the ObservableCollection until after the row is committed.
I'd like to make it so that new items are only added to the ObservableCollection after a full-row commit has occurred, meaning that each new item added to the Collection will already have data in it, instead of being inserted blank and then edited later.
I've searched high and low for an answer, but it appears that no one else is having this problem.
The reason why I want this functionality is that I am implementing an "Undo" feature in this DataGrid, but when new rows get added as blanks and then edited later, that always ends up counting as two changes, meaning the user needs to perform two "Undo" operations to remove a row they have just barely created. And that's not intuitive at all.
Thanks.
In general it's normal behavior of DataGrid.
DataGrid calls Add() for the new collection item when you start editing the row
DataGrid automatically calls Remove() for the item when you hit Escape key or call CancelEdit() in any other way
There's nothing strange to support (ignore in code) collection items with blank/default values. As alternative you may add some proxy collection and commit its changes to main collection when Datagrid saves the Row data. As for me, first way is preferrable.
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.
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.
I am loading elements (a row) from a database in a list. Which technique should I use to update the list when a new entry is added in that table ( SQL database) so that the lists updates and if I have selected an element from the list it won`t get deselected (like Outlook when new mails are received and you have a mail selected ).
If your dealing with simple database transactions, I would recommend a foray into the BindingSource Component. The BindingSource component can take care of creating, reading, updating and deleting things so you don't have to.
I assume your rows have a unique identifier. You can store this Id before you add the new row, and after it's added you can search the list with the stored Id to re-select that row.
(Actually it would be wiser to store the row handle and re-selecting it directly with the handle. But it wouldn't work if your grid creates new row objects to display the updated list. Since I don't know how you bind your data, I cannot say this would work for sure.)