Click here to Skip to main content
12,402,189 members (72,323 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# WPF
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" />

Posted 19-Oct-10 0:36am
Updated 19-Oct-10 0:37am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 1 Apr 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100