CRUD Operations in WPF With MVVM Framework: Home All Articles SQL Server Interview Questions
CRUD Operations in WPF With MVVM Framework: Home All Articles SQL Server Interview Questions
CRUD Operations in WPF With MVVM Framework: Home All Articles SQL Server Interview Questions
org
Your Problems Our Solutions
Log in
Entries feed
Comments feed
This Tutorial will explain how to implement crud operations in WPF with MVVM Framework. WordPress.org
Open Visual Studio 2012. Go to create new project tab. Select WPF Application.
MY FAVOURITES
Add Five folders to the Application.
Model
View
ViewModel
Data
Helpers
Employee Data
Bind this data grid to ViewModelUser. Write the below code to MainWindow.xaml.cs le.
Create a ViewModelUser.cs class inside ViewModel folder in your application. Add the below
code to this class le.
1 PersonnelBusinessObject personnel;
2 ObservableCollection<User> _Employee;
3 public ViewModelUser()
4 {
5 personnel = new PersonnelBusinessObject();
6 }
7 public ObservableCollection<User> Employee
8 {
9 get
10 {
11 _Employee = new ObservableCollection<User>(personnel.GetEm
12 return _Employee;
13 }
14 }
1 class PersonnelBusinessObject
2 {
3 List<User> Employee { get; set; }
4 public PersonnelBusinessObject()
5 {
6 Employee = DatabaseLayer.GetEmployeeFromDatabase();
7 }
8
9 public List<User> GetEmployees()
10 {
11 return Employee = DatabaseLayer.GetEmployeeFromDatabase()
12 }
13 }
DatabaseLayer class is used to communicate with database. you can write your stored
procedure and queries to fetch data from database.
Create a DatabaseLayer.cs class in Data Folder in your application. Write the below code in
this class.
1 class DatabaseLayer
2 {
3 public static List<User> GetEmployeeFromDatabase()
4 {
5 try
6 {
7 DataTable dt = SqlHelper.ExecuteDataTable(AppConstants.g
8 var Employee = new List<User>();
9 foreach (DataRow row in dt.Rows)
10 {
11 var obj = new User()
12 {
13 ID = (string)row["ID"],
14 FirstName = (string)row["FirstName"],
15 LastName = (string)row["LastName"],
16 DOB = (string)row["DOB"],
17 Gender = (string)row["Gender"],
18 Nationality = (string)row["Nationality"],
19 Language = ((string)row["Language"]),
20 Address = (string)row["Address"],
21 Male = (bool)row["Male"],
22 Female = (bool)row["Female"],
23 Hindi = (bool)row["Hindi"],
24 English = (bool)row["English"],
25 French = (bool)row["French"],
26 };
27 Employee.Add(obj);
28 }
29 return Employee;
30 }
31 catch (Exception ex)
32 {
33 throw ex;
34 }
35 }
36 }
After building and running the application data will be populate in the Data grid.
Implementing Insert, Update and Delete operations in WPF with MVVM Framework.
Employee Form
Open the MainWindow.xaml le and add the below code just above to the existing code.
1 <Window.Resources>
2 <!-- This converts SelectedItem to a collection, for use in
3 <helpers:SelectedItemToItemsSource x:Key="SelectedItemToItem
4
5 <!-- This is the template for the user form, used by the ite
6 <DataTemplate x:Key="UserGrid">
7 <Border Background="Chocolate" BorderBrush="Black" Borde
8 <Grid Margin="10">
9 <Grid.RowDefinitions>
10 <RowDefinition/>
11 <RowDefinition/>
12 <RowDefinition/>
13 <RowDefinition/>
14 <RowDefinition/>
15 <RowDefinition/>
16 <RowDefinition/>
17 <RowDefinition/>
18 <RowDefinition/>
19 <RowDefinition/>
20 <RowDefinition/>
21 </Grid.RowDefinitions>
22 <Grid.ColumnDefinitions>
23 <ColumnDefinition/>
24 <ColumnDefinition/>
25 </Grid.ColumnDefinitions>
26 <TextBlock Text="Emp ID" Grid.Row="1" Grid.Colum
27 <TextBlock Text="First Name" Grid.Row="2" Grid.C
28 <TextBlock Text="Last Name" Grid.Row="3" Grid.Co
29 <TextBlock Text="Address" Grid.Row="4" Grid.Colu
30 <TextBlock Text="Gender" Grid.Row="5" Grid.Colum
31 <TextBlock Text="Language" Grid.Row="6" Grid.Col
32 <TextBlock Text="Nationality" Grid.Row="7" Grid.
33 <TextBlock Text="DOB" Grid.Row="8" Grid.Column="
34 <TextBox Text="{Binding ID, BindingGroupName=Gro
35 <TextBox Text="{Binding FirstName, BindingGroupN
36 <TextBox Text="{Binding LastName, BindingGroupNa
37 <TextBox Text="{Binding Address, BindingGroupNam
38 <StackPanel Orientation="Horizontal" Grid.Colum
39 <RadioButton Name="radMale" GroupName="Gende
40 <RadioButton Name="radFemale" GroupName="Gen
41 </StackPanel>
42 <StackPanel Orientation="Horizontal" Grid.Colum
43 <CheckBox IsChecked="{Binding Hindi, Binding
44 <CheckBox IsChecked="{Binding English, Bindi
45 <CheckBox IsChecked="{Binding French, Bindin
46 </StackPanel>
47 <ComboBox DisplayMemberPath="Nationality" Select
48 <DatePicker x:Name="dpDOB" Grid.Column="1" Grid.
49 SelectedDateFormat="Short" SelectedD
50 <StackPanel Orientation="Horizontal" Grid.Row="1
51 <Button Foreground="White" Background="Green
52 <Button Foreground="White" Background="Green
53 <Button Foreground="White" Background="Green
54 </StackPanel>
55 </Grid>
56 </Border>
57 </DataTemplate>
58 </Window.Resources>
Add the below line of code just after the Data Grid.
Helper class is used to converts SelectedItem to a collection, for use in the ItemsControl.
Add the Helper class reference to the MainWindow.xaml.
1 xmlns:helpers="clr-namespace:WpfCrudeOperations.Helpers"
Create a Converters.cs class in the Helpers folder and add the below line of code in the class
le
Buttons “Add Employee”, “Save”, “Cancel” and “Delete” uses Relay command to communicate
with ViewModel.
Add the below line of code in ViewModelUser.cs class.
De ne methods
To Implement employee change event. add these line of code in ViewModelUser class
constructor.
1 BindingGroup _UpdateBindingGroup;
2 public BindingGroup UpdateBindingGroup
3 {
4 get
5 {
6 return _UpdateBindingGroup;
7 }
8 set
9 {
10 if (_UpdateBindingGroup != value)
11 {
12 _UpdateBindingGroup = value;
13 RaisePropertyChanged("UpdateBindingGroup");
14 }
15 }
16 }
To get the selected employee from the data grid. Add the below code in ViewModelUser
class.
Employee View
Related Posts:
1. Immutable Objects in java
2. CRUD Operations using ASP.NET Web API.
3. Asp.Net MVC CRUD Operations
4. CRUD Operations using WCF Rest API
This entry was posted in WPF and tagged Calender in WPF with MVVM, CheckBox in WPF with MVVM,
ComboBox in WPF with WVVM, Crud Operations in WPF, crud operations in wpf datagrid, Crud
Operations in WPF with MVVM Framework, delete in wpf with mvvm, Insert in wpf with mvvm,
RadioButton in WPF with MVVM., Select and Insert and Update and Delete in WPF, update in wpf with
mvvm, WPF Crud, wpf crud example, WPF Crud Operations, WPF Crud Sample, WPF Crud Tutorial on
January 28, 2015 by Hitesh Kumar.
About Hitesh Kumar
A Software Developer with more than 5+ years of rich experience in Software
Development in Microsoft Dot Net Technology.
View all posts by Hitesh Kumar →
neha
January 29, 2015 at 12:44 AM
In very Simple Way You discuss the WPF, It helps to build my rst WPF application.
javed
March 16, 2015 at 5:07 AM
Very Helpfull…………
Thanks For Sharing
Sunil
April 7, 2015 at 1:41 AM
Hi Sir,
very nice article, Please could you send me the source code of all, I am trying the above code
in my system but. I am not able to do it.
Please send me at [email protected]
Vineet
May 20, 2015 at 4:43 AM
Zalak Shah
July 18, 2016 at 1:42 AM
mahendra
November 30, 2015 at 11:56 PM
very nice article ,i have tried it but not able to implement it on my system,can you please
send whole source code on above mentioned email address
rajon
December 13, 2015 at 11:56 PM
how impliment EmployeeChanged method?please send me source code .
My email is [email protected]
sonali
May 29, 2016 at 1:02 PM
Rajon
December 14, 2015 at 11:51 PM
jyoti
December 28, 2015 at 5:40 AM
The article is very useful. I am implementing the code but at some places I m facing
problems. I would be grateful to you if you could send me the source code.
jyoti
December 28, 2015 at 6:36 AM
Nitin Patel
January 6, 2016 at 6:54 AM
Hitesh
January 7, 2016 at 3:45 AM
Nitin Patel
January 6, 2016 at 6:55 AM
Mouhssine CHOUKI
February 24, 2016 at 8:13 AM
Sheebu
March 3, 2016 at 1:59 AM
can you send sqlhelper class on this address : [email protected]
vijay
March 3, 2016 at 6:25 AM
Renato
March 3, 2016 at 9:41 PM
Its very good article. You can send the code to my email [email protected]
Fern Mart
April 7, 2016 at 10:59 AM
NK Kaushik
April 26, 2016 at 12:54 AM
NK Kaushik
April 26, 2016 at 6:15 AM
Nice article ! could you send me SCRIPT FOR stored procedure & table creation at thanks
[email protected]
Dnyanesh
May 10, 2016 at 9:48 AM
sonali
May 29, 2016 at 1:03 PM
Abdelhak tou
June 14, 2016 at 8:27 AM
tedwu
July 4, 2016 at 9:59 PM
very userfull for me!
Zalak Shah
July 18, 2016 at 1:44 AM
I want to show source code, can you please mail me the source code?
my email id is [email protected]
Thanks.
kiran
July 18, 2016 at 8:28 PM
kiran
July 19, 2016 at 3:58 AM
Nice article THANKS ! could you send me full source code @ [email protected]
Joe
July 28, 2016 at 9:21 PM
Harish
August 6, 2016 at 6:31 AM
AppConstants le contains only connection to database. You can simply add your
connection string.
Rahul
August 30, 2016 at 4:28 AM
legion
October 17, 2016 at 5:42 PM
Hi ,
Thanks for the great tutorial! Could it be possible to send me also the source code to
compare with mine?
Regards,
[email protected]
Rajendra valekar
October 27, 2016 at 6:20 AM
Govind Tupkar
November 10, 2016 at 3:15 AM