Click here to Skip to main content
14,937,188 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi all,

In my WPF application, I need to get all the selected rows of a datagrid on the click of a button which is outside the datagrid.I am using a checkbox in the row header template to select or deselect a row. Below is the small code snippet of my datagrid xaml. Please can any one help me out in fetching all the selected rows on the click of a button. I have searched a lot on google, but all the links are regarding fethcing selected items on "dataGrid_SelectionChanged" event, which is very easy.

<my:DataGrid AutoGenerateColumns="False" SelectionChanged="dataGrid_SelectionChanged" Initialized="DataGrid_Initialized" >

                        <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:DataGridRow}}}"/>
     <my:DataGridTemplateColumn Width="160" Header="Media Status" >
                         <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5,8,5,0">
                                        <DataTrigger Binding="{Binding MediaStatus}"  Value="False">
                                            <Setter Property="TextBlock.Text" Value="Not Processed" />
                                        <DataTrigger Binding="{Binding MediaStatus}" Value="True">
                                            <Setter Property="TextBlock.Text" Value="Processed" />

Updated 19-Oct-10 0:37am

Thanks everyone who are reading this post. I did it myself. I am posting the code below, for reference of others who need it. Please suggest if any good modification can be made to it.

private void btnProcessMedia_Click(object sender, RoutedEventArgs e)
                if (dgProjects.SelectedItems.Count > 0)
                    for (int i = 0; i < dgProjects.SelectedItems.Count; i++)
                        System.Data.DataRowView selectedFile = (System.Data.DataRowView)dgProjects.SelectedItems[i];
                        string str = Convert.ToString(selectedFile.Row.ItemArray[10]);

rajesh1986 10-Feb-12 3:29am
This is very useful code. I got this solution after a long watching.
Thank you
V G S Naidu A 30-May-12 1:33am
Thank you For your Post.. but it is useful when i using normal Coding i.e getting data From DataBase and append it to DataGrid....
But when i am using the entity FrameWork it didn't return the SelectedItems in the DATAGRIDROW format it, returns the objectType of the related table ...

what should i have to do when i am using the EntityFrameWork...
A better way:

foreach (var data in dataGridMain.SelectedItems)
    MyObservableCollection myData = data as MyObservableCollection;

Where "MyObservableCollection" is your data class that is bound to the DataGrid (in this case dataGridMain). And this collection has an author string for example.
Abubaker87 30-Sep-12 13:41pm
Thank You.. :) Excellent solution.. It worked...
Member 12115375 9-Mar-16 9:56am
Great :D Thnx!
Please read this link.
MultiItemSelect Behaviour in DataGrid [^]

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