Silverlight 4: Deleting Multiple Rows from the DataGrid

As a Silverlight 4 application developer, I use the Silverlight DataGrid control very frequently. The DataGrid is an excellent control provided for displaying data and manipulating it. One of the nice features of the Silverlight DataGrid is it helps us to add custom columns. Using these custom columns, we can add different controls in the column like a Button, Image, CheckBox, DropDownList etc. for DML operations, data representations and so on. Last week, while conducting a training at one of my clients, a requirement for deleting multiple rows from the DataGrid came up. In the code snippet given below, I have explained the technique of deleting multiple rows in a DataGrid.

Step 1: Create a Silverlight application, name it as ‘SL4_Deleting_Multiple_Rows_DataGrid’. To this project, add a class file to store data classes in it as shown below:

The above class file defines collection class for Employees.

Step 2: In MainPage.xaml, add the DataGrid from the toolbox. Create an instance of the above collection class in the XAML using resources. Add columns to the DataGrid. Make sure that the DataGrid defines Template Columns which will contain CheckBox. The XAML code is as shown below:

The above code defines an object of the type ObservableCollection<Employee>, ‘EmpToDelete’. This object will contain all Employee objects which are selected by the user by clicking in the CheckBox. The code ‘Delete Rows’ will delete all entries from ‘EmpCol’ Observablecollection<Employee> type by matching the related Employee entries from ‘EmpToDelete’ object.

Step 4: Run the application, click on the CheckBox of the specific row and then click on the ‘Delete Rows’ button, the following result will be displayed:

After clicking on ‘OK’ you will find the rows deleted:

Will you give this article a +1 ? Thanks in advance

About The Author

Mahesh is having 10 years of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions). He also blogs regularly at DotNetCurry.com. Follow him on twitter @maheshdotnet