Can't refresh datagrid - c#

I have a DataGrid, which loads the data on start up. There are several buttons on which the user can click. Each button updates the same column. The problem is that when the new value of that column is saved, the old value is still shown in the data grid. It must be refreshed. I have tried several ways to do it, like: t_KlantenDataGrid.Items.Refresh() and CollectionViewSource.GetDefaultView(t_KlantenDataGrid.ItemsSource).Refresh(). None of them works.
The code which loads the data:
OV.AOVDataSet aOVDataSet = ((AOV.AOVDataSet)(this.FindResource("aOVDataSet")));
// Load data into the table t_Klanten. You can modify this code as needed.
AOV.AOVDataSetTableAdapters.t_KlantenTableAdapter aOVDataSett_KlantenTableAdapter = new AOV.AOVDataSetTableAdapters.t_KlantenTableAdapter();
aOVDataSett_KlantenTableAdapter.Fill(aOVDataSet.t_Klanten);
t_KlantenViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("t_KlantenViewSource")));
t_KlantenViewSource.View.MoveCurrentToFirst();
I use Entity Framework. Why doesn't those two solutions work for me. Are there any other solutions to refresh the DataGrid?

If you see the old value with that type of data binding, it means you don't update the initial source, but only the temporary one (in your case t_KlantenViewSource). Calling Refresh method on the data added by FindResource cause app to reload it from pre-compiled resource, which won't change in this case.
In other words, the problem is you use pre-compiled resource
On the same time, you fill the dataset with actual data, but your dataset is in memory, view source - inside application file.
You can try:
binding ItemsSource of the entire datagrid or separate column to the TableAdapter dataset
change resource compiling behaviour to Content, thus the app will reload it from external file each time you call Refresh method.
ADDITION:
You can find a solution for your case, just to save time. Check paragraph "performing updates" Other way is to provide NotifyPropertyChanged Event for each bound parameter, but I can't say it is better in this case, assuming you update columns via unique buttons.
To be short, you need a method like this on your button clicked / property changed:
aOVDataSett_KlantenTableAdapter.Update(aOVDataSet.t_Klanten)

after button click first set datagrid datasource to null then assign the datas
datagrid.datasource=null

Thank you all for your answers. As Jasmine suggested, I reloaded the dataset and rebounded the datagrid. Maybe it's not the best way to do it but it was the only solution I had then.

Related

Best way to lazy load with TemplateStudio WinUI ListDetailsView

I started a project using TemplateStudio for WinUI (C#) 5.2. It uses the CommunityToolkit ListDetailsView Control for a "List Details" page.
What is the best way to load additional data when a user selects something from the list of results? I thought I would add two more properties to my ViewModel (one to handle the data, one to show a busy animation while the data is loading, both using ObservableObjects SetProperty), when the selected gets changed I fetch the data async for the selected item and update the ViewModel properties accordingly.
I can't seem to find a way how to access this data from the DetailsViewControl, and I'm not sure I do it the correct way anyway.
If I add a property to my model and change the value on changing the selected the data gets added, but the UI doesn't update itself (I tried _selected and Selected). Only when selecting another item from the results and going back to the previous shows the value.
What would be the best/correct way to lazy load additional data with this control? There would be about 4-5 parallel requests going on in the first release.
Later on I need a few buttons in the DetailsViewControl as well, where and how am I supposed to handle the commands?
Or am I better of not using this control and do it manually?

Hiding datagridview rows from user only when datasource is a bindingsource?

I had a pre-existing app with a datagridview in it. For a long time, calls to read or update the dgv were made by looping through rows and pulling cell values.
I decided a few weeks back that, since we ship this data to two foreign systems, it'd be nice to be able to access each row as an object. So, I created a bindinglist from the data and linked it to the DGV.
One of the problems I've encountered however, deals with hiding some of the rows: There is a set of radio buttons, "Internal", "External", and "Both" (the default). Historically, when users would click Internal or External, we'd loop through the dgv and adjust each row's visibility property (based on comparing with a checkbox column that represents the "Internal" option).
For reasons I won't go into, I thought it would be wise for me to remove the row-loop-visibility-change code. I saw that I could use BindingSource.Filter to filter the row visibility.
However, I'm having one problem, plus I have an additional question.
The problem: If I click back-and-forth between the three radio buttons repeatedly, it seems like I can't clear the filter properly. I've tried BindingSource.RemoveFilter() and BindingSource.Filter = null. I end up with a blank dgv no matter how which of the three options I press.
My question though is: Does this filtering only affect visibility to humans? While it's applied, could another thread come in and programmatically update the dgv? (I'm guessing it could update the datasource, but I'd probably have to make sure to wrap such updates with lock statements and Suspendbinding / Unsuspendbinding.)
Thanks!

How to restore changed properties of a data binded object?

I'm stuck and I hope for suggestions. I'm talking about only in-memory objects. No databases or files involved here.
Basically I have an object whose properties I've bound to some text- and combo-boxes. When I change the value in a texbox, the value in the bound property gets also changed, obviously. What I want to achieve is, that when I change the value in a texbox, the change should be made only temporarily.
This is what I do:
I load my data and then the main form gets opened with a list of all my data items. I double click an item and another form gets opened where I bind the properties of the item to some textboxes. I change something and click cancel. Now if I open again the same item, the changes are still there.
I want the changes to be temporarily so only if I click save, then the item should be updated. I can't figure it out, how to do it with a data bound object. I guess I miss the point of Databinding?
I could make it easy by assigning the values of my item properties to the textboxes and write the values in the texboxes back to the item properties as I click save. But isn't Databinding made for this kind of work or am I completely off?
Should I query my database again to restore the original data of that specific Item, if I want to use Databinding?
Edit: As suggested in the comments, I can use a copy of the item and save its changes to the original item, as I click save. But what is a good way to enforce that the form is working with copies of that item only? The view should not need to know whether its a copy or not or that it should make a copy first.

Navigation grid inside of a FormView

We have a list of elements we use to drive a form view. Typically the resultset is between 5-15 records. Right now in the form view we have the typical first/prior/next/last for navigation. The problem is that sometimes the user needs to go directly to a specific record to edit it. The records here are job tasks that have some pretty short descriptions.
The idea was proposed to put a gridview inside of the formview that lists all of the records for that form, and a user can just select view or edit on that record and it will navigate directly to that record and put it into the proper mode. We could put this outside the form view as well, that doesn't really matter.
The question is, regardless of the dirving force, how to I tell the formview to go to record X driven from something like an external grid.
I know that the formview has the DataKeyNames field but is there a way to say "Go to record who's PK is 17" given that it's in the current dataset for the formview?
If so, does anyone have any sample C# code for it? I know we could just populate the existing formview with a single record, but we also want to keep the normal navigation buttons in place as well, in the event as well (sometimes there's cases where there's hundreds of job tasks, in which case we'd supress the grid view -- sounds wrong but there's more to the business case).
This short solution was to embed the grid in the FormView ItemTemplate and reference the same data source for both.
To made the row editable, you need a simple callback for RowDataBound on the grid in which you will retrive the link button (or whatever control you're using to trigger the edit) and then set the command argument to the rowindex. After that, you need to have the callback for the link button (or trigger) that wil retrieve the command argument (again the row index) and then set the FormView PageIndex to that value, and then set the ChangeMode for the FormView to FormViewMode.Edit.
So to recap, for read only view it's a grid with all of the items, but when handle the edit or insert, you get the traditional form view.

Paste Excel data into WPF data Grid (or related object)

I'm trying to figure out a way to allow a user to paste data from the clipboard (specifically excel) into a WPF program. I figured I would need to use a DataGrid, but I can't seem to get it to go (and I was hoping to create an event handler to trigger after a user pastes something in). The data the user will paste in will be a single column with a fixed number of rows (ie 15 x 1), and will just be numbers.
Any thoughts? Thanks a lot!
Ctrl-c in Excel puts a CSV text (actually tab separated values into clipboard).
How exactly you implement the paste/import operation depends on the context. One option would be to handle paste action with an event handler on the grid and trigger the corresponding action in your view model. Another way would be to define a button or some other element so user could explicitly trigger paste (data import) operation.
However that action in the view model would take the data in the clipboard and parse it to create one instance of appropriate object for each row and add it to the ObservableCollection binded to the grid ItemsSource property.

Categories

Resources