How do I navigate a table using the entity framework? - wpf

I'm trying to create a simple table navigation screen in WPF using the entity framework on a database with one table, Students. The screen basically has the the students name and surname and back and forwards button.
The datacontext is pointing directly to the Students table and is setup as follows:
private DBEntities _entity = new DBEntities();
this.Datacontext = _entity.Students;
This works and I see the first entry on the table on the screen.
My problem is I can't see any way to navigate to the next entry when I click the next button.
There is First() method on Students but no Next().
All the solutions I've found via google dump the entire table in a List and navigate the list.
I'm wondering if there isn't a simpler way that I'm missing?

msdn topic here...
First get the collection as an ICollectionView,
ICollectionView view1 = CollectionViewSource.GetDefaultView(_entity.Students);
Now you can navigate the collection, in this case you can use MoveCurrentToNext

Related

How to reset a viewmodel in mvvm

How can I reset my viewmodel when the user clicks new button on the view that has the viewmodel as it's datacontext?
For example:
If I have a view NewCustomer and upon save, the data is saved to the DB and the newly created account number is displayed. But when the user clicks the New button in the screen, I want the view (viewmodel) to be reinitialized. Or if the user clicked cancel in the screen to clear all changes.
How can I achieve this? I am using Prism 5.0 and Unity as my container.
If I used IRegionMemberLifetime, I can clear the viewmodel data when I navigate away and navigate again to the view (by setting the KeepAlive as false on clicking New button before navigating away). But I want the form to be cleared without navigating. Can this be done?
You could have a screen/workspaceViewModel, and another ViewModel wrapping your data.
So two classes: CarScreenViewModel and CarViewModel.
The CarScreenViewModel would have a property, say CurrentCar, which reflects what is currently selected in the screen. Then, when clicking the Create button, you simply set:
CurrentCar = new CarViewModel();
Resetting partially loaded data will only lead to behaviour that is hard to reproduce. It is better to start with a fresh instance.
Your standard approach will be something like below
ViewModels
CustomersContainerViewModel which contains
a collection of CustomerViewModel s
and ICommands like
CreateNewCustomer
DeleteExistingCustomer
UpdateExistingCustomer
Your View will contain
the CustomersContainerView which will contain
a collection of Customer Objects in your required UI element
a button to Create new customer (which will launch a new screen which contains the newCustomer fields it can also contain cancel, which will just close the form)
a button to delete (can also be a ContextMenu)
a button to update (can also be a ContextMenu) which will launch a customer form filled with details from DB.
Hopefully this makes some sense... Let me know if you have problem with any of the above
Update - Forgot to add. NewCustomer Command will add a new Customer object to your CustomerCollection and that should open a NewCustomer form (or whatever you chose) for user to input the customer details. Cancel/Delete will just remove that record from the collection. Delete will update the DB as well in addition
In my case
yourViewName.variableName.postValue("")

ADF LOV Not Refreshing

I have an interesting situation. Working on an 11.1.1.3 ADF Application.
I have an editable table, which uses an LOV. The respective LOV has a maintenance page, where we populate a database table with data that the LOV uses.
If i'm inserting new records into the LOV Database Table, the corresponding LOV does not get those changes. However, if there is an UPDATE of existing records, it seems to work ok.
It appears the iterator is not updating. Because if i start up a new application session in a new browser window, i'll see the new changes.
The LOV doesn't have it's own iterator in the Page Bindings (Its using a View Accessor in my view object), so i'm not sure how to tell the iterator to refresh or executeQuery()...
Thank you!
Right now, my only solution is this. I don't like it, but it works...
Exposed View Row Impl class, and included accessors on my Editable ADF Table's View Object. This way i have programmatic access to my accessors that run the LOV.
Then i located my view accessor, getter method. And told it to re-execute the query. Worked like a charm:
public RowSet getAllSmsModules() {
RowSet rs = (RowSet)getAttributeInternal(SMSMODULESALL);
rs.executeQuery();
return rs;
//return (RowSet)getAttributeInternal(ALLSMSMODULES);
}

EF 4.1 local: when is it instantiaced?

I have a class, DataBaseManager, that use EF 4.1 to access to data base. This class hava a method search that search information in the data base. The resume version is that:
public ObservableCollecion<Authors> searchAuthtors()
{
_Context.Authors.SqlQuery("select * from authors").ToList<Authors>();
ColectionAuthors = _Context.Authors.Local;
return ColectionAuthros;
}
Also, this class has a property, _colAuthors, public, that I use to link external classes with this data manager. The idea it's, in WPF, use this _colAuthors to binding a dataGrid.
Well, in my ViewModel, in which I have a property Authors, which I use to binding the dataGrid of the View, in the constructor I do this:
public myViewModel()
{
_dataManager = new DataBaseManager();
Authors = _dataManager.ColectionAuthors;
}
I have the view, with a dataGrid, a button to update the changes and a button to search authors.
If first I search Authors, if in the dataGrid I delete, add or modifed items and later I click the button to update changes, it's works fine, add, delete or update the information and if I search again, I can see the correct information.
However, if I don't do a first search, I only add a register, because I don't have in the dataGrid registers to modify or delete. Well, if I add register and I click the update button, the changes has not been saved in the data base.
I think that this is because the context.Authors.Local is not been "create" until I make a first search, so when I do Authors = _dataManager.ColectionAuthors; I can't add the element to local, so when I do the savechanges() there is no elements in local to save in the data base.
I am right? is there any way to add elements to the context before doing the first search?
Thanks.
Daimroc.

Opening tab with tree node data

Hi,
I'm new to both Prism and WPF, and I have a question regarding sharing data between views.
Application I'm working on resembles SQL Server Development Studio. Demo contains two regions. First region contains tree (like Object explorer in SQL Server DS). Tree nodes are bound to different view models. Example would be DatabaseA->Tables->dbo.TableA->Columns etc.
Second region is initially empty. When I double click on tree node I would like to open view that displays data I clicked in tree.
In detail:
1. double click on tree node
2. node data and display it in second region
3. if second region isn't empty, check if clicked node data is already displayed in one of existing tab pages
4. if not, create new tab page with clicked node data, otherwise focus existing tab page
Until now, I managed to created tree. When tree node is clicked app calls:
UriQuery uriQuery = new UriQuery {{"ID", unit.Id.ToString()}};
uriQuery.Add("TypeName", "Unit");
var uri = new Uri("DebugTreeItemView" + uriQuery, UriKind.Relative);
RegionManager.RequestNavigate("SECOND_REGION", uri);
This will open view with tab control and I can fetch uri parameters. But I'm not satisfied with this solution. I need a way to:
1. intercept this RegionManager.RequestNavigate call in order to check if tab control is alread created. Also, I need to check that clicked node data isn't already displayed in one of existing tab pages.
2. I would like to send Unit object directly to tab control view instead of sending ID and typename. What is the best way to achieve this?
Thanks.
I would have a ShellViewModel controlling my overall application. It would contain the TreeNode collection for the left side, and the OpenTabs collection for the right side. It would also contain the SelectedTabIndex for the right side, and perhaps the SelectedTreeNode for the left side if it made sense to do so.
When you want to open a new Tab, I would use the EventAggregator to publish an OpenTab event and pass it the selected TreeNode item. The ShellViewModel would subscribe to those events, and would determine if that object already existed in the OpenTabs collection. If it exists, it simply sets the SelectedTabIndex. If not, it adds the item to the OpenTabs collection before setting the SelectedTabIndex
A while back I posted something here on this sort of navigation with MVVM if you're interested

How can i use CompositeCommand for saving a NEW employee with details

I have a module which has a multiple tabs. Tab1-Employee details, Tab2 - Employee Assignments, Tab3 - Employee Vacation.
The module should allow the user to enter details in multiple tabs and click on SaveAll button. The problem is the employeeid is generated by saving the data on first tab. This is required for saving other tabs. How can i achieve this scenerio? Should i use Composite command/Region context or something else?
Appreciate your response.
Thanks.
I'm not sure whats the problem here.
To save all the information you need to save the information from the first tab and then the others, thats no problem. If you're using MVVM (wich you should) then the ViewModel should have access to the data, and be able to save the data.
Then it is just a matter of how the view is designed, doesn't matter if it is a TabControl or not. Just put a Command (just the usual DelegateCommand) inside the ViewModel and save first the information of the first tab. After that save the other information using the employeeid just generated.
With this scenario you could also add some validation to the ViewModel (see INotifyDataErrorInfo) and desactivate or activate the command whenever you want.

Resources