Hi,
i am using a datagrid control using 3 columns first column with checkbox header as well as rows if i select header checkbox i need all the rows to be selected and if i unselect all the rows to be unselected and also i have a delete button outside of the datagrid. if i check header checkbox and perform delete operation i need all the rows to be delete and if i select individual record should be delete.how to acheive this .Thanks

Amit GuptaRank #190 · 12.8K reputationPosted Dec 15, 2016, 10:51 PM EST
Shweta LodhaRank #31 · 39.8K reputationPosted Dec 15, 2016, 10:58 PM EST
Hi,
A complete sample is given over here: http://www.devcurry.com/2011/04/wpf-4-datagrid-delete-multiple-rows.html
DataGridRowEditEndingEventArgs e)
{
FrameworkElement element = dgEmp.Columns[4].GetCellContent(e.Row);
if (element.GetType() == typeof(CheckBox))
{
if (((CheckBox)element).IsChecked == true)
{
FrameworkElement cellEmpNo =
dgEmp.Columns[0].GetCellContent(e.Row);
int EmpNo = Convert.ToInt32(((TextBlock)cellEmpNo).Text);
lstSelectedEmpNo.Add(EmpNo);
}
}
}
AshokRank #2,671 · 521 reputationPosted Dec 19, 2016, 8:30 PM EST
AshokRank #2,671 · 521 reputationPosted Dec 20, 2016, 7:43 PM EST
Amit GuptaRank #190 · 12.8K reputationPosted Dec 20, 2016, 10:17 PM EST
AshokRank #2,671 · 521 reputationPosted Dec 22, 2016, 8:20 PM EST
Shweta LodhaRank #31 · 39.8K reputationPosted Dec 26, 2016, 5:17 AM EST
Hello Sai,
You need not to call the event explicitly. In xaml, simply register this at DataGrid level as
AshokRank #2,671 · 521 reputationPosted Jan 2, 2017, 1:29 AM EST
Shweta LodhaRank #31 · 39.8K reputationPosted Jan 2, 2017, 1:36 AM EST
Shweta LodhaRank #31 · 39.8K reputationPosted Jan 2, 2017, 6:55 PM EST
sai,
As you mentioned that you have a checkbox at header level. So, in that case you can take a property SelectAll in your viewModel and update your item source collections like:
public bool SelectAll { get{return this._selectAll;} set { this._selectAll = value; this.MyItemsSourceCollection.ForEach(x=>x.MyRowCheckProperty=value); this.OnPropertyChanged("SelectAll"); } }Shweta LodhaRank #31 · 39.8K reputationPosted Jan 2, 2017, 6:56 PM EST
One readymade solution is also available here:
https://www.codeproject.com/questions/484312/howplustoplusselectplusallpluscheckboxplusofplusap