SIGN UP MEMBER LOGIN:    
ARTICLE

CRUD Operation in DataGrid in WPF

Posted by Diptimaya Patra Articles | WPF with C# February 17, 2010
In this article you will learn how to use CRUD Operation in DataGrid in WPF.
Reader Level:
Download Files:
 

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:

image6.gif

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.

image7.gif

Create and Update Operation

The following code describes the Create and Delete Operation.

image8.gif

Delete Operation

The following code represents the Delete Operation.

image9.gif

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

Hope this article helps.

Login to add your contents and source code to this article
share this article :
post comment
 

i have a question. i am trying to update the row in a background worker and before updating i am setting isEnabled to false because its a long update task. but if row is updating i can not select or edit another row even my update function is in the background worker, how can i achieve that functionality. regards

Posted by Syed Waqsa Feb 27, 2012

when i try to insert new row, the Id cell set value 0 when it have to be the next number because it's identity, how can i fix that. i can't insert any row for that

Posted by alexis vidal Sep 10, 2011

Hi Diptimaya,
  Great article, but my question is: Is possible CRUD with RowdetailsTemplate? Have you any sample code for this?
Thank you.

Posted by carlos cabrera May 21, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor