ComboBox Items Disappearing while Re binding - c#

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.

Related

Cant remove data from DataGrid

I have a WPF app, with datagrid which I use for displaying data from database (Enitity Framework Core).
I want to add button for clearing the datagrid (remove all data). But all the options for removing don't work. What am I missing here?
dbContext.myData.Load();
myDataGrid.ItemsSource = dbContext.myData.Local.ToBindingList();
I press a button to call DataGrid clean
myDataGrid.ItemsSource = null;
myDataGrid.Columns.Clear();
myDataGrid.Items.Clear();
myDataGrid.Items.Refresh();
But nothing gets removed. Why is that?
You should clear from both datagrid and database object.
you have done the data grid part, clear the database object too.
(didn't try but maybe setting itemsource to an empty new object also do the same)

Add item update listbox

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

Datagrid is not updating rows in WPF MVVM

In my application I have two datagrids connected to DB by Entity Framework. First datagrid is used only to select worker, second datagrid is to add, remove, update previous selected workers training's. Please check the screenshot for better view.
Problem is that whenever I add or remove rows from second datagrid (with training's) It's not updating rows, it update database, but datagrid control stays the same. Modifying rows works good.
What I have done while trying to solve this:
1. Checking for stupid code mistakes (misspeling etc)
2. Force to update Itemsource with : OnPropertyChanged("ToolboxList");
3. Breakpoint everything and it works...
But datagrid rows are the same...
Some clues that might be the reason but I am not sure if they are problem makers here:
enovaWorkers its not a table its a view, hr_ToolboxTalk its a table, and because you can't make association view to table they are associated with one to many pragmatically inside EF edmx file.
Second datagrid is not binded by selection from first datagrid (SelectedWorker.hr_ToolboxTalk) because whenever you want to edit row in second grid it raises Edit is not allowed exception. This is described here: master detail datagrid not editable
And since I cant and don't want to edit entitiy framework tt file I made workaround with GetToolboxTalkList() method. So please check the code.
Pasted code (some sections are cuted):
XAML : pastebin.com/h3sdbfKm
ViewModel: pastebin.com/7Vj3P5UJ
ICommand class: pastebin.com/rQAh7FM9
Thanks for the help...
Change:
List<hr_ToolboxTalk> ToolboxList;
to:
ObservableCollection<hr_ToolboxTalk> ToolboxList;
It will work, Lists dont Notify to the view Automaticaly.

Removing an element from the DataGridView.DataSource error

I have a Windows Form application that has a DataGridView control with a List DataSource.
I must remove some elements from the DataGridView and I use the list to do that like this:
MyList.Remove(SpecificItem);
MyDataGridView.Invalidate(true);
MyDataGridView.Refresh();
And when I want to scroll all the way down to the last item in the DataGridView I get the IndexOutOfRange error. Even if I call the MyDataGridView.Update(); I still get the same error.
How can I get around this error? Thanks.
Since MyDataGridView.DataSource is not bound to the List you need to set the MyDataGridView.DataSourceeach time the List changes. Using Invalidate() or Refresh() doesn't do anything in this case.
This msdn link Bind Data to the Windows Forms DataGridView Control might help you to bind your data to the DataGridView

Update DataGridView with updated View automatically

I am using View as a Data Source to my DataGridView and I want the DataGridView to update automatically every X time with an updated View, and Its a problem because I am using View instead of SQL command.
As I've searched so far, I need to refresh the data source in the gridview to show updated data and it should be like that:
GridView.DataSource = null;
GridView.DataSource = ViewDataSource;
This does refresh the GridView, but with the same data.
the problem is that the view itself doesn't change, even thought I wrote:
ViewDataSource.EndEdit();
I am pretty sure its because I havn't start editing it and as I know the view was taken as the program started, so I am looking for a way to refresh my view (its my datagridview's data source) with the updated data so I can add it to the grid.
Thanks!
GridView.Refresh() and add Application.DoEvents() for it to refresh immediately.

Categories