To clear current selection, you can use this code (as you see it is different whether the mode is Single or Extended)
if(this.dataGrid1.SelectionUnit != DataGridSelectionUnit.FullRow)
this.dataGrid1.SelectedCells.Clear();
if (this.dataGrid1.SelectionMode != DataGridSelectionMode.Single) //if the Extended mode
this.dataGrid1.SelectedItems.Clear();
else
this.dataGrid1.SelectedItem = null;
To select new items programmatically, use this code:
if (this.dataGrid1.SelectionMode != DataGridSelectionMode.Single)
{ //for example, select first and third items
var firstItem = this.dataGrid1.ItemsSource.OfType<object>().FirstOrDefault();
var thirdItem = this.dataGrid1.ItemsSource.OfType<object>().Skip(2).FirstOrDefault();
if(firstItem != null)
this.dataGrid1.SelectedItems.Add(firstItem);
if (thirdItem != null)
this.dataGrid1.SelectedItems.Add(thirdItem);
}
else
this.dataGrid1.SelectedItem = this.dataGrid1.ItemsSource.OfType<object>().FirstOrDefault(); //the first item