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)
Related
I want to create an application that is refreshing DataGridView content with data about surrounding Wifi networks on every second. I wonder what would be the best binding solution for this task. I think using BindingList is meaningless, because it would update the datagridview on every single update on the List, while it would be better to refresh it after whole List is updated. Meaby simple List and rebinding is appropriate here, what do you think?
You can prevent the DataGridView from updating on every change to your BindingList by the following
myBindingList.RaiseListChangedEvents = false;
// Update BindingList
myBindingList.RaiseListChangedEvents = true;
myBindingList.ResetBindings();
The ResetBindings() call will cause your DataGridView to be refreshed to reflect the changes to myBindingList.
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.
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.
I have a bindingsource which has a datasource which is a datatable.
All the winforms controls has added databindings to the bindingsource
I do a value change in the GUI (writes to the controls' .text property)
then on Save i do following
bsSending.EndEdit();
((DataRowView)this.bsSending.Current).Row) now contains the new values, but the RowState is still unchanged. How can this be possible? I haven't any calls to AcceptChanges() before I make the value changes in the GUI
UPDATE:
Don't know why, but it seems that calling the specific row's EndEdit does the trick. The row's parent is a datatable and the table's dataset is the datasource of the bsSending Datasource.
Calling bsSending.EndEdit() only updates the values but doesn't update the rowstate.
I have surfed the .net for similiar problems and they indicate that when calling AcceptChanges() on the dataset BEFORE binding data, then you may get this error (values updated to dataset but rowstate remains unchanged). I haven't seen any solutions to the problem, though, so I keep my workaround solution
I know it's old post.
It can be solved by calling DataRowView.EndEdit directly, but in my case I found the exact reason:
I accidentally bound two properties of one control to different columns.
(In my case I simultaneously bound Devexpress' TextEdit control - EditValue and Text properties to different columns of the underlaying table).
Maybe it will help someone even in 2016+, because it was nasty bug to catch.
I have a similar issue. I have a grid on the first tab page and textboxes on the second tab page, all binded to the same binding source.
I change the row content, the content get changed in the grid but the row start is Unchanged.
My code was:
DataRow dataRow = ((DataRowView)bindingSource1.Current).Row;
if(dataRow.RowState != DataRowState.Modified)
I expected to have a Modified rowState.
The missing code was:
bindingSource1.EndEdit();
Entire solution here:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex == 0)
{
bindingSource1.EndEdit();
DataRow dataRow = ((DataRowView)bindingSource1.Current).Row;
if(dataRow.RowState != DataRowState.Modified)
{
return;
}
DialogResult userOption = MessageBox.Show("Save?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (userOption == System.Windows.Forms.DialogResult.Yes)
{
Save();
}
}
}
My solution:
I have run into the similar situation in one form and I realized that problem was in binding made to labels ToolTip property. First I realized, that when I changed value for null from null to some string, the problem disappears, but after some other changes made to the project, the problem appeared again. When I deleted binding to the ToolTip property, problem disappeared again. Still I do not know if for ever.
#Jan Strnad 's answer enlightened me and helped me fix my issue.
Problem: I was using a binding source in win forms. When binding the controls in the form, I miss clicked and bound a field to the form's Text property in (DataBindings). The same bindingsource field was bound (correctly) to a combobox. Because of this double binding on the field, the rowstate never changed from Unmodified.
Fix: when rows don't update and you use a binding source, first check that the binds are correctly put in place.
Yes, I have the same experience. I am using Visual Studio 2010 and .NET Framework v.4.0. I am working with DataGridView control which is bound to a DataTable.
If the user tries to close the form while the cell value is still in edit mode, I want DataGrid to end the edit mode and ask the user whether he wants to save or lose the changes.
This is the code which works for me:
if (dgv.IsCurrentCellInEditMode)
{
dgv.EndEdit();
updatedData.Rows[dgv.CurrentCell.RowIndex].EndEdit();
}
if (updatedData.GetChanges() != null && updatedData.GetChanges().Rows.Count > 0)
{
// if there are changes, update the dataset
}
I want to point out that I needed to call both - EndEdit() on DataGridView to let it end the editing. Then call EndEdit() on the DataTable to mark the row 'modified'.
To commit row changes programmatically, call the form's Validate method. If your data source is a BindingSource, you can also callBindingSource.EndEdit.
see
IsCurrentRowDirty
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.