WPF Binding on Entity Framework - wpf

I have MS SQL Server database with one table - Peoples. From these database generated Entity Framework Data model.
In WPF application I have ListBox with ItemSource = DataModel.Entities.Peoples, and two buttons - add and remove People in database.
Add button:
DataModel.Entities.AddPeople(new People("test"));
DataModel.Entities.SaveChanges();
And removing:
DataModel.Entities.Remove((People)listBox1.SelectedItem);
DataModel.Entities.SaveChanges();
When I click remove button - corresponding People row removing from databases and listBox1 refreshing. But when I click add button - People added in database (see in MS SQL Enterprise Manager), but listbox does not refreshing.
How to refresh listBox on adding? Guess I forgot to set any option in DataModel?

Unless DataModel.Entities.Peoples is an ObservableCollection, it won't be aware of changes.
I recommend you use the MVVM pattern for this, which solves that problem perfectly.

Related

Filtration for List view data upon List view Header column clicks in WINFORMS

I dont know whether this is possible or not, But i just want your suggestions on this ..
Here is what I would like to be able to do. Not sure if this functionality is built in or if I will need to use a combination of any controls/tools.
I want to be able to filter a listview data.. have it filter the listview accordingly on click of listview columns header.I want to be able to use checkboxes in dropdown of listview header column such that I can filter data easily.
Best Example: Control Panel-->Uninstall a Program ,Where a list of uninstall programs available in listview for computing filtration upon click of any header.
I want this in Windows Forms Applications C# Visual Studio 2010
If solution is possible, pls. share the site links or any free tools also as well.
Here is the same idea for a DataGridView http://www.codeproject.com/Articles/33786/DataGridView-Filter-Popup However, you would have to write the code to do the filtering as a winforms ListView does not supporting binding to a DataTable.
ObjectListView -- an open source wrapper around a .NET ListView -- supports filtering out of the box.

updating database table from observable collection

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.

What is the best way to make a wizard using native WPF controls

I'm new to WPF and never happened the need for me to create a wizard in WPF. I want to know what control to use to make a simple wizard in WPF that I can collect information on each page and finalize an operation in the last page(step). I actually want to make it using native WPF controls. I don't know, like using page navigations or so. Any native wpf ideas?
You can make use of Tab Control for making a wizard.
For moving to the next screen on a particular index can bind the SelectedIndex property of the tab control to a property in the view model and if you do not wish to display the tab items in UI can set its height to 0.
Check out the WPF Extended toolkit. It has a built in Wizard control.
Wizard

Datagrid Edit Windows Form

I have worked in ASP.net before and know that I can edit its equivalent to a datagrid (detail view or something like that?) However, I'm working on a windows form now and i need to use the datagrid. I know there are options to edit each individual cell and when I ran my program, it edited it, but it does not update to the database. I was wondering how I would be able to write a SQL code using ADO.net for the datagrid so I can update certain things (or I can bind the datagrid but maybe there is a build in edit command) thanks!
Databinding is significantly different in WinForms than it is in ASP.NET. There's no request cycle and databinding is always both ways (controls read data from the datasource and automatically update it as well).
However you'll still need to take the data from your datasource and persist it to the database. How you do that depends on what your datagrid is bound to.
I recommend you review this MSDN article Windows Forms Data Binding and the links that matter to you

How to View, Edit and Update a Database Table with WPF's DataGrid?

am a newbie in the WPF and more so the WPF Datagrid arena. Am so used to working with the cousin -- DatagridView from Windows Forms, but it's my first time working with WPF's Datagrid Control, and am having a very hard time!
I have created a dataset using the in-built datasource wizard in Vidual Studio 2010. After creating this dataset, I used another in-built feature that allows me to auto-generate a datagrid that is pre-bound to this a table in the dataset, so I can just drop it onto the window.
Now, all that is done nicely, til I come to the part where I need to have the user edit the contents of the datagrid, and via a callback, have the new / updated data committed to the underlying table in the database.
I've tried searching around, but most articles are out-dated, and some don't hit the problem in ways that make sense to a newbie like me.
One source says:
DataGrid checks for
IEditableCollectionView’s CanAddNew,
CanCancelEdit, and CanRemove
properties before executing the
EditItem, CancelEdit, or CommitEdit
methods. So if editing appears to not
work for some reason, be sure to check
that it is able to edit.
from an MSDN source, but the Auto-generation feature of Visual Studio 11 gives me this for the data-binding source
<Window.Resources>
<my:crimexDataSet x:Key="crimexDataSet" />
<CollectionViewSource x:Key="datapoolViewSource" Source="{Binding Path=datapool, Source={StaticResource crimexDataSet}}" />
</Window.Resources>
So, how do I move from this to an IEditableCollectionView kind datasource, so I can have editing enabled? Thanks in advance...
If this isn't an application with a short lifetime, I would save myself a lot of pain and learn about the MVVM (Model-View-ViewModel) pattern. It is a very common pattern in the WPF and Silverlight worlds.
Basically, you would make a class specifically designed to be the data source for the grid, and perhaps other data you would need in your window.
For this particular need, you would probably use an ObservableCollection as the type of the property bound to the grid.
There is a lot of information on MVVM here at StackOverflow, to be found on Google, and in books. If you grok the pattern, it can really make the UI development experience a lot less painful.

Resources