.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 >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Delete Multiple rows from DataGrid Using CheckBox in Silverlight

Posted By:Diptimaya Patra       Posted Date: March 27, 2010    Points: 25    Category: Silverlight    URL: http://www.dotnetspark.com  

Delete Multiple rows from DataGrid Using CheckBox in Silverlight. In this article we will see how we can have a CheckBox Column. And with the help of CheckBox we can multiselect rows and perform delete operation.
 

Introduction


Previously we have seen how to select a row with the help of RadioButton Column. In this article we will see how we can have a CheckBox Column. And with the help of CheckBox we can multiselect rows and perform delete operation.


Creating Silverlight Project


Fire up Visual Studio 2008 and create a new Silverlight 3 Project. Name it as MultiSelectDeleteInDataGrid.


1.gif
 
Open the Solution in Expression Blend and add some useful controls such as a DataGrid and a Button.


2.gif

 
As you see from above image, I have customized the DataGrid with Template Columns. Follow the below XAML.


           
               
                   
                       
                           
                       
                   
               
               
               
               
     
               
               
           


Now we will create sample class that will hold the properties and we will create sample data to be shown in DataGrid.


public class Person
{
   public string Name { get; set; }
   public int Age { get; set; }
   public string Country { get; set; }
   public string Gender { get; set; }
   public string EmailId { get; set; }
   public DateTime JoinedOn { get; set; }
}
ObservableCollection myList;
       ObservableCollection personList = new ObservableCollection();   
  public MainPage()
       {
           InitializeComponent();
           myList = new ObservableCollection()
           {
               new Person{ Name="Person 1", Age=21, Country="India", Gender="Male", EmailId="some@some.com", JoinedOn=DateTime.Now},
               new Person{ Name="Person 2", Age=29, Country="India", Gender="Male", EmailId="some@some.com", JoinedOn=DateTime.Now},
               new Person{ Name="Person 3", Age=20, Country="India", Gender="Male", EmailId="some@some.com", JoinedOn=DateTime.Now},
               new Person{ Name="Person 4", Age=22, Country="India", Gender="Male", EmailId="some@some.com", JoinedOn=DateTime.Now},
               new Person{ Name="Person 5", Age=23, Country="India", Gender="Female", EmailId="some@some.com", JoinedOn=DateTime.Now},
               new Person{ Name="Person 6", Age=23, Country="India", Gender="Male", EmailId="some@some.com", JoinedOn=DateTime.Now},
         new Person{ Name="Person 7", Age=25, Country="India", Gender="Female", EmailId="some@some.com", JoinedOn=DateTime.Now},
               new Person{ Name="Person 8", Age=25, Country="India", Gender="Female", EmailId="some@some.com", JoinedOn=DateTime.Now},
               new Person{ Name="Person 9", Age=24, Country="India", Gender="Male", EmailId="some@some.com", JoinedOn=DateTime.Now}
           };
           dgPerson.ItemsSource = myList;
}

Now if you run your application you can see the populated data in DataGrid.

3.gif

Now we will write few event handlers and add some code into it. 
Such as:
  • DataGrid [LoadingRow]
  • CheckBox [Checked], [Unchecked]
  • Button [Click]

We would load the CheckBoxes IsChecked property to false when the DataGrid is reloaded. We would add or remove the selected row content to a list based on check or uncheck of CheckBox. And finally we will perform the Delete operation.


private void chkPerson_Checked(object sender, RoutedEventArgs e)
{
   if (this.dgPerson.SelectedItem is Person)
   {
   personList.Add(((Person)this.dgPerson.SelectedItem));
   }
}
private void chkPerson_Unchecked(object sender, RoutedEventArgs e)
{
   if (this.dgPerson.SelectedItem is Person)
   {
   Person person = this.dgPerson.SelectedItem as Person;
       var item = personList.First(i => i.Name == person.Name);
       personList.Remove(item);
   }
} 
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
   string persons = string.Empty;
   foreach (var item in personList)
   {
   myList.Remove(item);
   }
   dgPerson.ItemsSource = myList;
   personList.Clear();
} 
private void dgPerson_LoadingRow(object sender, DataGridRowEventArgs e)
{
   CheckBox chk = (CheckBox)this.dgPerson.Columns[0].GetCellContent(e.Row);
   chk.IsChecked = false;
}

We are ready for testing our application. Run the Application.
4.gif


 
As you see from the above image we have multiselected. Now press Delete Selected button.


5.gif

 
The selected rows are deleted. That's it. Hope this article helps.

 Subscribe to Articles

     

Further Readings:

Responses
Author: Vivek         Company URL: http://www.dotnetspark.com
Posted Date: September 23, 2010

Dear Diptimaya,
Thank you for your wonderful post, it helped me a lot in my development.
the problem which m i facing still is as follows.
Kindly go through the following:

Lets try and add few more Persons let's say 50 in numbers, so that we have to scroll a bit means the loading row event get's call for multiple times say for another 3-4 times

and assume that i have selected "Person 3, 5, 7" and now I scroll down to select say "Person 47 & 48"

but when i again scroll back/UP to "Person 3, 5, 7" then they appear as UNCHECKED & i scroll down to reach "Person 47 & 48" then they are also UNCHECKED...

could you pls guide me through this satuation

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