I have bound a Data Grid to an ObservableCollection. It fills the grid with more than 200 records. Now user makes changes using editable control provided in the grid and finally clicks the save button to update the data back to database but I am not getting the idea how to update the modified collection back to the database because there is no PRIMARY KEY in the table.
The table has four columns apart from created by and modified by.
Like this i think i'll have to keep two collections. One older collection to compare against a new modified collection. I am quite new to think of better ideas.
What could be a simple and better approach?
You should refer to this video by Nikhil Kothari Developing with WCF Ria Services quickly and effectively.
There are several others on YouTube.
Related
I am new to WPF, MVVM and EF and I want to develop an application using these technologies.
Simply lets say I have Customer and Orders tables in my DB.
I managed to generate the model using EF. I need a tutorial or example that enables me to learn how to do retrieve Customers and Orders in 2 grids and when selecting a customer it updates the ordergrid to get orders related to that customer.
I'd like to have add/update/delete in both grids.
How to bind a column in a grid to a lookup table?
I believe this is exactly what you are looking for:
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx#dataset
Under the section "Binding to a Dataset", the project uses a Customer table and an Order table, and shows examples of databinding.
Edit: I don't believe you need to install the WPF toolkit for the datagrid (as the tutorial claims), as it is included in WPF 4.
Trying to create what i feel should be an easy WPF application. basically what i want it to do is have a autocomplete combobox which is bound to a sql db table. the table has 2 fields(Address and Directions)
once the user selects the record from the address combox the directions textbox below will be filled directions stored in the table.
if there are no records in the address combobox the user can then type in the combobox and add the new entry.
there will be a save button on the form that saves the new entry and will be visible in the list. Im new to WPF applications and have been trying to get this to work for weeks. Any help would be appreciated.
I would start by checking out the following discussion which will point you to several good tutorials on using the MVVM (Model-View-ViewModel) pattern with WPF.
MVVM: Tutorial from start to finish?
Other than that, you haven't really given any indication what you've tried and what isn't working so I can't really offer any other advice. But even the simplest applications in WPF are much cleaner and more easily implemented with MVVM than using code behind and drag and drop data binding.
I am using a WCF service reference in a WPF project, and my entity framework data model resides in WCF project.
And I am using MVVM Light framework. I am doing the following things:
I use LINQ in the service to get data and then fetch it from WPF, obersvablecollections usually.
Everything works in view part like populating datagrid, views as required.
But I have following doubts:
Is this correct way of transferring data between wcf and wpf.
I haven't used the Model for anything yet, I have doubt about when to use it?
I also wanted to save data from datagrid. I was able to pass on the observablecollection of updated data of datagrid to the service's function. But how do i update the entity from this collection? by looping? doesnt sound right. Once I update the entity from this collection I will be able to use saveChanges to update into database.
When I need to show hierarchal data in a treeview, where to make that data hierarichal, from stored procedure xml? use a view to create a grouping criteria column? create this column in service? create this column/property in presentation?
1 - There is no correct way, it depends on your requirements and goals.
2 - With MVVM, the model should sit between WPF and the database. That means all calls to the database should go through the model, and all writes to the database should also go through the model. The WPF GUI should only bind to the model. This usually means that your WPF portion consists mostly of XAML code. All code that accesses the database should be in the model.
There are good reasons for separating this.
You can write unit tests that on the model.
The view model is independent from the look of the GUI. This means that you can easily change the GUI by dropping in different components and just binding to the model.
A quick google search can probably yield more reasons.
3 - I would try to send over only the entities that have changed. This can be done by passing the collection to your view model, and have your view model figure out what has changed.
4 - I don't quite understand what you want to do. Usually, to make a TreeView, you should create HierarchicalDataTemplate for each of your view models. The TreeView control will take care of the rest. You should really do some tutorials on this one, because it's kinda hard to wrap your head around.
In looking through samples I have seen a number of examples where it is possible to present data within a wpf applicaiton by binding to collections. However I was wondering is it possible to write to a collection from an applicaiton. Say for example i have 2 or more fields such as names and I wanted to have hte ability to add up to three names in my application ( all stored in memory). Will collections serve this purpose. In the past with asp.net I have done this by creating data tables and storing values on the fly or during the session. I am trying to learn WPF and I was wondering if collections work in the same fashion?
If so could you please post an example or point me to references that show examples of this?
Thank you in advance.
Use ObservableCollection as a datasource if you want UI to update on the changes you made to collection from your code.
See ObservableCollection<(Of <(T>)>) Class It also contains quite a good sample.
I am breaking up my project in small sections and one of the sections involves loading a grid with possibily lots of records (could be up to 1000s of records in the database).
Ideally I would like some sort of mechanism where as the users scrolls the grid, more data is retrieved.
I have read that certain controls (datapager with RIA) do this but I would like to know how I could implement this myself or do something similiar?
I was thinking about first loading 50 records at a time and when the user gets to scroll near the 50th record, then get another 50 as a start and so on. Not sure how I do this but this does not feel right or whether I should load ids of records in the grid and then get each row to load itself via an async thread but then I am hitting my database for each record?
Thanks
JD.
Sounds like you are describing data virtulization. Xceed seems to be working on a commercial control with this capability but to date have not realeased anything(see Xceed DataGrid for Silverlight Tech Preview! ). For some ideas from the WPF world on doing this you can check out:
Data virtualization
How can I improve on existing WPF data virtualization solutions?
I have just published a couple of articles with some sample code describing how to do stealth paging/data virtualization in silverlight without using any third-party components.
Read the introductory post to find out about the sample, and then follow up with Digging into VirtualCollection.