How to check all rows in DataGrid by clicking Check all Button in WPF in C#
-
HI, This is from Chandrakanth. Working on WPF Project. Actually i have one CheckButton called as CheckALL. I have one DataGrid. DataGrid Consist of some rows. I want to Click on that CheckAll Button than all the rows in DataGrid should check. How can i write that in WPF. Can any one tell me about that. Thanks and Regards Chandrakanth Responses
-
HI, This is from Chandrakanth. Working on WPF Project. Actually i have one CheckButton called as CheckALL. I have one DataGrid. DataGrid Consist of some rows. I want to Click on that CheckAll Button than all the rows in DataGrid should check. How can i write that in WPF. Can any one tell me about that. Thanks and Regards Chandrakanth Responses
You would have a boolean property on the data, let's call it Selected. Then you bind your checkbox to the property, and your code simply updates the data - leaving the binding engine to ensure that the checkbox is updated as a result. Don't try to do something like this in the UI, use the data to control it.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
You would have a boolean property on the data, let's call it Selected. Then you bind your checkbox to the property, and your code simply updates the data - leaving the binding engine to ensure that the checkbox is updated as a result. Don't try to do something like this in the UI, use the data to control it.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
HI, Pete O'Hanlon, Thaks for the early reply. Once Again from Chandrakanth. Actually i have bind the data to the DataGrid Control. I have 4 rows in that dataGrid with every row containing CheckBox in the first Column. My Problem How to Check all the rows in the DataGrid, when i click on CHECKALL button.Actully i was tried with some code to check all Rows in DataGrid. But Unable to that. i need help on that. If you have any code on that Please reply to me. Thanks and Regards Chandrakanth
-
HI, Pete O'Hanlon, Thaks for the early reply. Once Again from Chandrakanth. Actually i have bind the data to the DataGrid Control. I have 4 rows in that dataGrid with every row containing CheckBox in the first Column. My Problem How to Check all the rows in the DataGrid, when i click on CHECKALL button.Actully i was tried with some code to check all Rows in DataGrid. But Unable to that. i need help on that. If you have any code on that Please reply to me. Thanks and Regards Chandrakanth
I've already told you how to do it. Don't try to programatically check items in the grid, use the model to do this. Suppose you have a model that looks like this:
public class MyItem : INotifyPropertyChanged
{
private bool _selected;
private string _name;
private event PropertyChangedEventHandler _changed;
public event PropertyChangedEventHandler Changed
{
add { _changed += value; }
remove { _changed -= value; }
}public bool Selected
{
get { return _selected; }
set
{
if (_selected != value)
{
_selected = value;
OnChanged("Selected");
}
}
}
// Name code ommitted for brevity...
}
public class MyViewModel
{
private ObservableCollection<MyItem> _items = new ObservableCollection<MyItem>();public void HandleCheckAll
{
foreach (MyItem item in _items)
{
item.Selected = true;
}
}public ObservableCollection<MyItem> Items
{
get { return _items; }
}// Some code to populate the items would need to go here....
}All you need then is a command that calls
HandleCheckAll
and the grid binding to theMyViewModel.Items
. The checkbox is bound (two-way) toSelected
, and job's done."WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.