0

I want to be able to select multiple rows in this DataGrid, then retrieve the data present in the selected cells. Everything I have found online isn't working. I've tried to use event arguments like 'SelectionChanged' and 'SelectedCellsChanged' to get the rows index, but it doesn't behave in a manner that allows me to keep track of all the rows that are selected.

Is anyone able to suggest a way of doing this? Thanks in advance.

DataGrid code:

<DataGrid x:Name="walkGrd" ItemsSource="{Binding overviewGrd}" RowHeaderWidth="0" AutoGenerateColumns="True" ColumnWidth="*" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserSortColumns="False"  IsReadOnly="True" SelectionMode="Extended" SelectionUnit="FullRow" Margin="1,63,0,37"  RowHeight="18" FontSize="13" BorderBrush="Black" AreRowDetailsFrozen="True">
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridCell}">
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Margin" Value="0,0,-1,0"/>
                <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="DarkGray"/>
                        <Setter Property="Foreground" Value="White"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
            <Style x:Key="Body_Content_DataGrid_Centering"
                TargetType="{x:Type DataGridCell}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Grid Background="{TemplateBinding Background}">
                                <ContentPresenter VerticalAlignment="Center" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Identifier" TextBlock.TextAlignment="Justify" Binding="{Binding one}" Width="0.25*" />
            <DataGridTextColumn Header="ID OID" Binding="{Binding two}" Width="0.25*" />
            <DataGridTextColumn Header="Up Time" Binding="{Binding three}" Width="0.25*" />
            <DataGridTextColumn Header="Time OID" Binding="{Binding four}" Width="0.25*" />

        </DataGrid.Columns>
    </DataGrid>
clomas
  • 93
  • 1
  • 13

2 Answers2

0
private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //Get SelectedItems from DataGrid.
    var grid = sender as DataGrid;
    var selected = grid.SelectedItems;

    foreach (var item in selected)
    {
        var item = item as ClassName;
    }
}

After you have set your SelectionMode to Multiple

boop_the_snoot
  • 3,209
  • 4
  • 33
  • 44
Allanckw
  • 641
  • 1
  • 6
  • 17
  • Thanks for the prompt response, I don't understand what the following line does though? are you able to clarify? Thanks! var item = item as ClassName; – clomas Sep 15 '17 at 02:43
  • It is to loop through your Selected Items, and cast the item as ClassName, to an object preferably through a class. It would work if your datasource is of an IENumberable Object – Allanckw Sep 15 '17 at 05:18
0

I found the solution in this post - DataGrid get selected rows' column values

It also handles if a row is deselected.

private void walkGrd_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
    foreach (var item in e.AddedCells)
    {
         var col = item.Column as DataGridColumn;
         var fc = col.GetCellContent(item.Item);

         if (fc is TextBlock)
         {
             Console.WriteLine("Values" + (fc as TextBlock).Text);
         }
         //// Like this for all available types of cells
    }
    foreach (var item in e.RemovedCells)
    {
         var col = item.Column as DataGridColumn;
         var fc = col.GetCellContent(item.Item);

         if (fc is TextBlock)
         {
            Console.WriteLine("Values Removed" + (fc as TextBlock).Text);
         }
         //// Like this for all available types of cells
    }   
}
boop_the_snoot
  • 3,209
  • 4
  • 33
  • 44
clomas
  • 93
  • 1
  • 13