This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Data Display Controls - DataGrid - Selection

The one hundred and thirteenth part of the Windows Presentation Foundation Fundamentals tutorial further investigates the DataGrid control. This instalment looks at the selection capabilities of the control.

Detecting Selections

Once the user has selected cells or rows in a data grid, you might wish to determine which items are selected programmatically. You can do this by reading the DataGrid class's SelectedCells property. This returns a list of DataGridCellInfo values, with one for each selection. Each object includes properties that you can use to find the location of the selected cell and its content.

The following key properties can be interrogated:

  • Column. Returns the column containing the selected cell. You can find details of the column using the properties that we've seen in earlier articles. The returned value is a DataGridColumn object. This type includes the GetCellContent method, which returns the content of the selected cell.
  • Item. Returns the item in the data bound collection that is represented by the row containing the selected cell.
  • IsValue. This Boolean property returns true when the selected cell contains valid information. If the value is unset, or is null, the property returns false.

To demonstrate how the selections can be obtained, we'll add a button to the window. When the button is clicked we'll show the details of the selected cells in a message box.

To begin, replace all of the window's XAML with the following code, to add the button in a new Grid cell.

<Window x:Class="DataGridColumnsDemo.MainWindow"
        Title="Planets of the Solar System"
        <CollectionViewSource x:Key="SortedPlanets" Source="{Binding}">
                <scm:SortDescription PropertyName="Name" Direction="Ascending" />

            <RowDefinition />
            <RowDefinition Height="22"/>

        <DataGrid Name="SampleGrid"
                  ItemsSource="{Binding Source={StaticResource SortedPlanets}}"
                  AutoGenerateColumns="False" Hyperlink.Click="DataGrid_Click"
                  SelectionMode="Extended" SelectionUnit="CellOrRowHeader">
                <DataGridTextColumn Binding="{Binding Name}" Header="Planet" Width="80"
                                    Foreground="Blue" FontWeight="Bold"
                <DataGridCheckBoxColumn Binding="{Binding HasRings}" Header="Rings?"
                                        Width="50" CanUserResize="False"/>
                <DataGridCheckBoxColumn Binding="{Binding GlobalMagneticField}"
                                        Width="140" Header="Global Magnetic Field?"
                <DataGridComboBoxColumn Header="Category" Width="80"
                                        SelectedItemBinding="{Binding Category}">
                            <sys:String>Gas Giant</sys:String>
                            <sys:String>Ice Giant</sys:String>
                            <sys:String>Dwarf Planet</sys:String>
                <DataGridHyperlinkColumn Binding="{Binding MoreInfoUri}" Header="More"
                                         Width="80" ContentBinding="{Binding Name}"
                    SortMemberPath="Name" />
                <DataGridTemplateColumn Header="Moons" Width="50" SortMemberPath="Moons">
                            <TextBlock Text="{Binding Moons}" />
                                    <ColumnDefinition Width="Auto"/>
                                <TextBlock Text="{Binding Moons}" />
                                <ScrollBar Grid.Column="1" Minimum="0" Maximum="100"
                                           SmallChange="1" Orientation="Vertical"
                                           Value="{Binding Moons,
                                        <ScaleTransform ScaleY="-1"/>

        <Button Grid.Row="1" Width="100" HorizontalAlignment="Right"
                Click="Info_Click">Show Selection</Button>

We can now add the Click event code for the button behind the window. Add the following code:

private void Info_Click(object sender, RoutedEventArgs e)
    var selectedItems = SampleGrid.SelectedCells.Select(c => CellData(c));
    string msg = selectedItems.Aggregate((c, n) => c + Environment.NewLine + n);
private string CellData(DataGridCellInfo cell)
    return string.Format("{0}/{1} = {2}"
        , ((Planet)cell.Item).Name, cell.Column.Header
        , cell.Column.GetCellContent(cell.Item));

When the button is clicked, the first method gets the selected cells and produces a string representing each one. It uses the Aggregate method to combine the strings before showing them.

The CellData method creates the strings from three pieces of DataGridCellInfo information. To identify the column, we read the Column property and extract its Header, as shown at the top of the grid. The row of the selected cell is identified by reading Item, which returns the correct Planet object. The string.Format method includes the Name property from that Planet.

The final text added into the string is the content of the selected cell. Note how the GetCellContent method of the column is used to obtain this, with the Planet passed to the parameter of the method to identify which object contains the underlying data. The content will not be a plain string. It will contain the details of the controls that are present within the cell. For example, for the Name column the method will return a TextBlock.

Run the program, make some selections and click the button to see the results.

Selecting and Deselecting All Cells

The final two members that we'll consider in this article allow you to programmatically select or deselect every cell within the DataGrid. To add every cell to the current selection, call the SelectAllCells method. To deselect every cell, you can call UnselectAllCells.

To demonstrate, let's add two more buttons to the window. Add the following XAML beneath the existing button:

<Button Grid.Row="1" Width="100" HorizontalAlignment="Right"
        Margin="0 0 200 0" Click="SelectAll_Click">Select All</Button>
<Button Grid.Row="1" Width="100" HorizontalAlignment="Right"
        Margin="0 0 100 0" Click="UnselectAll_Click">Unselect All</Button>

Next, add the code to select and deselect the cells in response to the two Click events:

private void SelectAll_Click(object sender, RoutedEventArgs e)

private void UnselectAll_Click(object sender, RoutedEventArgs e)

You can now run the program and click the buttons to see the effects of the two methods.

20 November 2014