.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

Select / Deselect all checkbox in DataGrid In Silverlight

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

Select / Deselect all checkbox in DataGrid In Silverlight. In this article we will see how we can select all rows of DataGrid and delete all selected.
 

Introduction


In the previous article we have seen how to add a Checkbox Column In DataGrid and how to achieve multi select delete operation. In this article we will see how we can select all rows of DataGrid and delete all selected.


Creating Silverlight Project


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


1.gif

 
First we will design our application to have a DataGrid to display data and a Button to Delete Selected Rows.


2.gif

 
As you see we have customized the Columns in the DataGrid.


Now we will add a CheckBox Control in the Header of first column.


I tried many ways to put a CheckBox over there, but the effective way is to create a style and assign the style to HeaderStyle of that Column.


Here is the Style in XAML.






Now assign the Style to your HeaderStyle of the Column.



           
               
                   
                       
                           
                       
                   
       
               
               
               
               
               
               
           


Now Run the Application to see the Header CheckBox.


3.gif

 
Now we will create a class that will hold the Properties of sample data and create sample data to populate the 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; }
   }
     //List for Original List of Persons
       ObservableCollection myList;
       //List for selected Persons
       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;
       }
//We will add the event for LoadingRow for the DataGrid.
private void dgPerson_LoadingRow(object sender, DataGridRowEventArgs e)
{
   CheckBox chk = (CheckBox)this.dgPerson.Columns[0].GetCellContent(e.Row);
   chk.Click += new RoutedEventHandler(chkPerson_Click);
   Person p = chk.DataContext as Person;
   chk.IsChecked = personList.Contains(p);
}

//Now we will add the Click event handler to the Header CheckBox and the CheckBox in DataGrid Rows.

void chk_Click(object sender, RoutedEventArgs e)
{
   CheckBox chk = sender as CheckBox;
   bool check = chk.IsChecked.Value;
   if (check)
   {
   foreach (Person p in myList)
       {
       personList.Add(p);
           chk = dgPerson.Columns[0].GetCellContent(p) as CheckBox;
           if (chk != null)
           chk.IsChecked = true;
       }
   }
   else
   {
   foreach (Person p in myList)
       {
       chk = dgPerson.Columns[0].GetCellContent(p) as CheckBox;
       if (chk != null)
       chk.IsChecked = false;
       }
       personList.Clear();
   }
} 
private void chkPerson_Click(object sender, RoutedEventArgs e)
{
   CheckBox chk = sender as CheckBox;
   bool check = chk.IsChecked.Value;
   Person p = chk.DataContext as Person;
   if (check)
   {
   if (!personList.Contains(p))
   personList.Add(p);
   }
   else
   {
   personList.Remove(p);
   }
}

//Now we will have the Delete Button Click handler.

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();
}


Now we will run the application and test the application.



4.gif

 
If we click on the Select CheckBox in the header of the First Column, all the rows would be selected.


5.gif

 
Now if we don't need everything to be selected but few, then we would uncheck the respective rows.


6.gif

 
Now we would delete the selected rows.


7.gif

 
The selected rows got deleted.


Hope this article helps.

 Subscribe to Articles

     

Further Readings:

Responses
Author: akhtar         Company URL: http://www.dotnetspark.com
Posted Date: July 14, 2010

Dear Mr.Diptimaya Patra ,
i read your article it is very helpful for me but there is a problem when i select the all check box and maximize the page some time all check boxes are checked some time few are unchecked
kindly help me
thanks
Author: Kennetsider         Company URL: http://www.dotnetspark.com
Posted Date: February 04, 2011

Brilliant article. Thanks so much for taking the trouble to post this.

Following it not only gave me exactly the grid result I was looking for, it also exposed me to many fundamental techniques which will be a great help in all my coding.



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