Deleting Row With SelectionUnit=Cell

May 3, 2009 at 2:45 PM
I have a DataGrid and a button which called a command. The command looks like this:

if(dgResources.SelectedValue == null)
string selectedValue = dgResources.SelectedValue.ToString();
foreach(SomeObjectType res in anObservableCollection) {
if(res.Name == selectedValue) {
dgResources.ItemsSource = resources;
But when I change the SelectionUnit to Cell, neither the button nor the Del key delete the current row any more. If I could get the index of the row, or the cell I could probably delete it another way but all of the SelectedValue/SelectedIndex etc properties are null.
May 3, 2009 at 3:34 PM
That is by design.  The DeleteCommand for DataGrid works when a row is selected.  One way to work around this is to intercept the delete key press and do the delete yourself based on the row of the selected cell (similar to what you are doing above).
May 3, 2009 at 6:35 PM
I just needed to use the right properties. The SelectedCell's item can be removed from the DataSource:

if(dgResources.SelectedCells.Count == 0)
SomeObject selectedValue = dgResources.SelectedCells[0].Item as SomeObject;