Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my DataGrid, how do I know if the user has selected or deselected all rows of the DataGrid ?

Thanks


XML
<DataGrid ItemsSource="{Binding Dati_Viag}" SelectedItem="{Binding SelectDat}" Style="{DynamicResource ST_DataGrid}"
          CellStyle="{DynamicResource St_DataGridCellStyle}" SelectionMode="Extended" Name="Dg_Dati" >
   <DataGrid.Columns>
        <DataGridTextColumn x:Name="col_A" Binding="{Binding Path=A}" Header="A" Width="250" />
        <DataGridTextColumn x:Name="col_U" Binding="{Binding Path=B}" Header="B" Width="250" />
        <DataGridTextColumn x:Name="col_K" Binding="{Binding Path=C}" Header="C"  Width="250" />
   </DataGrid.Columns>
</DataGrid>
Posted
Comments
Christian Amado 2-Jan-15 9:26am    
Handle the SelectionChanged event of your DataGrid.

1 solution

Hi,
SelectionMode="Extended" are OK there. You can write this line of code inside of your Selection_Changed event from the DataGrid:
C#
private void Dg_Dati_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (this.Dg_Dati.SelectedItems != null) //Avoid exception
    {
        if (this.Dg_Dati.SelectedItems.Count == 0)
        {
            MessageBox.Show("The user has deselected all rows");
            return;
        }

        if (this.Dg_Dati.SelectedItems.Count == this.Dg_Dati.Items.Count)
        {
            MessageBox.Show("The user has selected all rows");        
            return;
        }
    }
}

Hope it helps.
 
Share this answer
 
Comments
abarosini 2-Jan-15 15:57pm    
Hi, Christian.
Thank you very much for your answer.
The code is ok, but I work with ItemsSource="{Binding Dati_Viag}" and use Selection_Changed event is not good.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900