5

I would like to know how to select multiple rows with DataGridCheckBoxColumn .

Here I'm able to select only one row, but how to do multiple selection.

My XAML is as follows:

<UserControl.Resources>
    <Style x:Key="itemstyle" TargetType="{x:Type DataGridRow}">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightGoldenrodYellow" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
        </Style.Resources>
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="ItemsControl.AlternationIndex" Value="1" />
                    <Condition Property="IsSelected" Value="False" />
                    <Condition Property="IsMouseOver" Value="False" />
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="#EEEEEEEE" />
            </MultiTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

<Grid Width="500" Height ="300">
    <DataGrid ItemsSource="{Binding Path=Script}" HeadersVisibility="Column" SelectionMode="Single" AlternatingRowBackground="Gainsboro" Background="White" AutoGenerateColumns="False" ItemContainerStyle="{StaticResource itemstyle}" CanUserAddRows="True" GridLinesVisibility="None" Height="242" HorizontalAlignment="Left" HorizontalContentAlignment="Left"  IsEnabled="True" IsReadOnly="True"   Margin="10,14,0,44" Name="dgMain" RowHeight="23" VerticalAlignment="Center" VerticalContentAlignment="Center"  Width="478" >
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDoubleClick">
                <i:InvokeCommandAction Command="{Binding EditData}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <DataGrid.Columns>
            <DataGridCheckBoxColumn Binding="{Binding Path=IsSelected}" Header="Select" Width="50" />
            <DataGridTextColumn Binding="{Binding Path=Script_Text}" Header="Script" Width="400" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

thanks

SN

2 Answers 2

3

You can do it, credit goes to Scott

https://blog.scottlogic.com/2008/11/26/multiselect-datagrid-with-checkboxes.html

And the trick is to use RowHeaderTemplate like below

<DataGrid ItemsSource="{Binding}">
    <DataGrid.RowHeaderTemplate>
      <DataTemplate>
         <Grid>
             <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay,
                     RelativeSource={RelativeSource FindAncestor,
                      AncestorType={x:Type dg:DataGridRow}}}"/>
          </Grid>
</DataTemplate>

Sign up to request clarification or add additional context in comments.

Comments

1

SelectionMode="Extended" or SelectionMode="Multiple" will make your DataGrid multiselect

7 Comments

you are not able to select mutliple rows in DataGrid?
Yes, only one row can be selected !! When I select the 2nd one, already selected item is un selected :(
that is weird.. how are you trying to select the rows.. by dragging clicking and dragging mouse over rows?
I try to select by Click on the Checkbox. When click on the next row item, already selected item unchecked.
when I select rows with Shift Key, multi slection is possible. not with one by one.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.