Click here to Skip to main content
Rate this: bad
good
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" >
 
<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 19-Oct-10 1:36am
Edited 19-Oct-10 1:37am
v2
Rate this: bad
good
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]);
                    }
                }
            
        }
 

Anurag
  Permalink  
Comments
rajesh1986 at 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 at 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
good
Please Sign up or sign in to vote.

Solution 2

A better way:
 
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.
  Permalink  
Comments
Abubaker87 at 30-Sep-12 13:41pm
   
Thank You.. :) Excellent solution.. It worked...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

System.Data.DataRowView selectedItem = (System.Data.DataRowView)item;
YourDataSet.YourRow r = selectedItem.Row as YourDataSet.YourRow ;
MessageBox.Show(r.Name);
  Permalink  
v2
Comments
CHill60 at 11-Sep-13 14:02pm
   
Posting solutions to questions that are 3 years old and with an accepted answer usually only attracts downvotes
gamadesigne at 20-Sep-13 20:14pm
   
hhhm, yes okay i know but for the others to make the answer so rich
thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

For getting the cell value from the selected rows folowing code is used:
 
http://niravdaraniya.blogspot.in/2014/06/how-to-get-column-value-from-selected.html
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

for (int i = 0; i < dg_facturas.Items.Count; i++)
{
    CheckBox mycheckbox = 
dg_facturas.Columns[0].GetCellContent(dg_facturas.Items[i]) as CheckBox;
 
    if (mycheckbox.IsChecked == true)
    {
        string folio =    
((System.Data.DataRowView)(dg_facturas.Items[i])).Row.ItemArray[3].ToString();
 
 
    }
 
}
  Permalink  
Comments
CHill60 at 3-Feb-15 9:48am
   
There really isn't any point in answering a 4 year old question that already has an accepted answer.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 7,448
1 OriginalGriff 5,996
2 Peter Leow 4,292
3 CHill60 2,858
4 Mika Wendelius 2,850


Advertise | Privacy | Mobile
Web04 | 2.8.150224.1 | Last Updated 3 Feb 2015
Copyright © CodeProject, 1999-2015
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