Click here to Skip to main content
15,886,637 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.

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

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






Thanks
Anurag
Posted
Updated 19-Oct-10 0:37am
v2

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.


C#
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]);
                    }
                }
            
        }



Anurag
 
Share this answer
 
Comments
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:

MSIL
foreach (var data in dataGridMain.SelectedItems)
{
    MyObservableCollection myData = data as MyObservableCollection;
    MessageBox.Show(myData.author);
}


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.
 
Share this answer
 
Comments
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 [^]
 
Share this answer
 

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