WPF

WPF DataGrid – Selection Mode

WPF DataGrid provides different properties to select, unselect different rows and columns of grid.

Single/Extended

The SelectionMode property is used for selecting single/multiple row and columns.

This property is an enum which have two values:

  1. Single
  2. Extended (Default)

Single

Single allows you to select only one row or column in the DataGrid. Column is only selected when SelectionUnit is set to ‘Cell’.

<DataGrid x:Name="myDataGrid" SelectionMode="Single">
</DataGrid>

DataGrid SelectionMode Single

Extended

Extended allows you to select multiple rows or columns in the DataGrid. You can use Shift or Ctrl keyboard keys to select multiple rows.

Shift + DownArrow OR Shift + UpArrow allows you to select continuous grid rows.

Ctrl + Mouse Click on any row allows you to select non-continuous grid rows.

<DataGrid x:Name="myDataGrid" SelectionMode="Extended">
</DataGrid>

DataGrid SelectionMode Extended

SelectionUnit

SelectionUnit property of DataGrid is used for selecting rows in more granular level.

This is an enum property and provides three different selection options:

  1. Cell
  2. FullRow (Default)
  3. CellOrRowHeader

Cell

By default, DataGrid allows you to select full row. You cannot select single cells. But If you change SelectionUnit to ‘Cell’, It allows you select single cell also.

<DataGrid x:Name="myDataGrid" SelectionUnit="Cell">
</DataGrid>

DataGrid SelectionUnit Cell

FullRow

FullRow is the default SelectionUnit mode. It does not let you select you any single cell. If you click on individual cell or RowHeader, it will select full row like shown below.

<DataGrid x:Name="myDataGrid" SelectionUnit="FullRow">
</DataGrid>

SelectionUnit FullRow

I have selected single cell in name column ‘George’, but full row is selected automatically.

CellOrRowHeader

In the option, you have both options for selection individual cell or selection of full row.

<DataGrid x:Name="myDataGrid" SelectionUnit="CellOrRowHeader">
</DataGrid>

SelectionUnit CellOrRowHeader

In the above screenshot, In second row I have selected single column and In the third row, I have selected full row.

Row Header is the very first column in the WPF DataGrid. It is mainly blank and use for selecting full Row.

Select, Unselect Rows/Columns from Code-Behind

WPF DataGrid provides four different methods for you to select and unselect rows/columns from code-behind class.

  1. SelectAll
  2. UnselectAll
  3. SelectAllCells
  4. UnselectAllCells

SelectAll, UnselectAll Methods

SelectAll, UnSelect methods is used for selecting all rows and columns in Grid.

private void SelectAll_Click(object sender, RoutedEventArgs e)
{
	myDataGrid.SelectAll();
}

private void UnSelectAll_Click(object sender, RoutedEventArgs e)
{
	myDataGrid.UnselectAll();
}

SelectAllCells, UnselectAllCells Methods

These method is used for selecting all rows and column in Grid. These methods are only useful when SelectionUnit is ‘Cell’.

private void SelectAll_Click(object sender, RoutedEventArgs e)
{
	myDataGrid.SelectAllCells();
}

private void UnSelectAll_Click(object sender, RoutedEventArgs e)
{
	myDataGrid.UnselectAllCells();
}

SelectAll method will only work when SelectionUnit is FullRow or CellOrRowHeader. If you call SelectAll method when the SelectionUnit is ‘Cell’, then this method will throw an exception ‘InvalidOperationException’. You have to use SelectAllCells and UnselectAllCells methods in this case.

Get Selected Row(s)/Column(s) from WPF DataGrid

DataGrid provides three options for getting selected rows.

  1. SelectedItem
  2. SelectedItems
  3. SelectedCells

SelectedItem

SelectedItem returns the selected row from the grid. If there are multiple selected rows, then it will returns the first selected row. If no item is selected, it returns null.

If you have chosen DataGrid SelectionUnit to ‘Cell’, then SelectedItem always returns null.

If you have chosen DataGrid SelectionUnit to ‘CellOrRowHeader’, and you have selected some individual cell and some row, then SelectedItem returns always the first selected row.

private void Button_Click(object sender, RoutedEventArgs e)
{
	Employee selectedEmployee = myDataGrid.SelectedItem as Employee;
}

SelectedItems

SelectedItems return the list of selected rows. If no row is selected it returns blank list.

If you have chosen DataGrid SelectionUnit to ‘Cell’, then SelectedItems always returns null.

If you have chosen DataGrid SelectionUnit to ‘CellOrRowHeader’, and you have selected some individual cell and some rows, then SelectedItems returns all selected rows.

private void Button_Click(object sender, RoutedEventArgs e)
{
	List<Employee> employees = new List<Employee>();

	IList items = myDataGrid.SelectedItems;
	foreach (object item in items)
	{
		employees.Add(item as Employee);
	}
}

SelectedCells

If you have chosen SelectionUnit to ‘Cell’, then for getting selected cells you have to use SelectedCells property of DataGrid.

This returns the List of DataGridCellInfo. DataGridCellInfo contains two important properties.

  1. Column – Returns the selected Cell
  2. Item – Returns the binded item to row.

DataGridCellInfo.Column has the Header property which returns the selected cell header name. Below is the example for getting selected cells.

private void Button_Click(object sender, RoutedEventArgs e)
{
	IList<DataGridCellInfo> cells = myDataGrid.SelectedCells;
	foreach (DataGridCellInfo cell in cells)
	{
		string header = cell.Column.Header as string;
		Employee item = cell.Item as Employee;
	}
}