.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

CRUD Operation In DataGrid In WPF Tutorial

Posted By:Diptimaya Patra       Posted Date: April 17, 2010    Points: 25    Category: WPF    URL: http://www.dotnetspark.com  

CRUD Operation In DataGrid In WPF Tutorial. I have been getting requests to write article on DataGrid CRUD operation without using any other control. So this article is for all the requestors. I have used only DataGrid for CRUD operation.
 

Introduction

I have been getting requests to write article on DataGrid CRUD operation without using any other control. So this article is for all the requestors. I have used only DataGrid for CRUD operation.

Crating WPF Application Project

Fire up Visual Studio 2008 and Create a WPF Application and name the project as CRUDDataGridWPF.

image1.gif

We would use Linq to SQL as our ORM. So add like below figure.

image2.gif

Now after adding, open the Server Explorer and add the required table, in our case EMP_BASIC_INFO.

image3.gif

We would change the table names as follows:

image4.gif

Now in Blend I have designed the WPF Application like the below figure.

image5.gif

The design displayed above contains a DataGrid for CRUD Operations and a TextBlock to display status messages.

It's below the DataGrid.

The following XAML code is for your reference:


        
    	
    		
    		
    	
        
            
                
                
                
                
                
                
        
        
        


We will perform the operation in below order:
  1. Read
  2. Create and Update
  3. Delete
Read Operation

The following code describes the read operation.

        #region Constructor
        public Window1()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Window1_Loaded);
        } 
        #endregion

        #region Read Operation
        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            EmployeeDBDataContext context = new EmployeeDBDataContext();
            var result = from emp in context.Employees
                         select emp;
            if (result.ToList().Count > 0)
            {
                txtStatus.Text = "Success: Read Operation";
            }
            dgData.ItemsSource = result.ToList();
        }
        #endregion


Create and Update Operation

The following code describes the Create and Delete Operation.

        #region Create and Update Operation
        private void dgData_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            if (e.EditAction == DataGridEditAction.Commit)
            {
                EmployeeDBDataContext context = new EmployeeDBDataContext();
                Employee emp = e.Row.DataContext as Employee;

                var matchedData = (from em in context.GetTable()
                                   where em.ID == emp.ID
                                   select em).SingleOrDefault();

                if (matchedData == null)
                {
                    Table empTable = context.GetTable();

                    Employee employee = new Employee();
                    employee.FirstName = emp.FirstName;
                    employee.LastName = emp.LastName;
                    employee.EmailID = emp.EmailID;
                    employee.Contact = emp.Contact;

                    empTable.InsertOnSubmit(employee);
                    empTable.Context.SubmitChanges();

                    txtStatus.Text = "Success: Data Inserted";
                }
                else
                {
                    matchedData.FirstName = emp.FirstName;
                    matchedData.LastName = emp.LastName;
                    matchedData.EmailID = emp.EmailID;
                    matchedData.Contact = emp.Contact;
                    context.SubmitChanges();

                    txtStatus.Text = "Success: Data Updated";
                }
            }
        } 
        #endregion


Delete Operation

The following code represents the Delete Operation.

        #region Delete Operation
        private void dgData_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            EmployeeDBDataContext context = new EmployeeDBDataContext();
            Employee employee = dgData.SelectedItem as Employee;
            
            if (employee != null)
            {
                var matchedCustomer = (from em in context.GetTable()
                                   where em.ID == employee.ID
                                   select em).SingleOrDefault();
                if (e.Command == DataGrid.DeleteCommand)
                {
                    if (!(MessageBox.Show("Are You Sure you want to Delete ?", 
                        "Confirm Delete !", MessageBoxButton.YesNo) == MessageBoxResult.Yes))
                    {
                        e.Handled = true;
                    }
                    else
                    {
                        context.Employees.DeleteOnSubmit(matchedCustomer);
                        context.SubmitChanges();
                        txtStatus.Text = "Success: Selected Data Deleted.";
                    }
                }
            }
        }
        #endregion


Now run the application and all the CRUD operations are successfully used.

Note: Code is not written for multi select delete. It can be done the same way single select, try doing it. You can also download sample application used in above example.

Hope this article helps.



 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend