Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have DataGrid in Window and I put column inside the DataGrid type "DataGridCheckBox",and I have button in the same Window, but the problem is I don't know how can get index all the rows which user is checked when user click this button. the code is :
XML
<Window x:Class="BenashManage.DeletePerson"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
<Grid HorizontalAlignment="Right" Margin="0,0,0.4,-0.4" Width="546" >
  <DataGrid Margin="15,104,13.6,48.8" Grid.Row="1" Name="GridEdite" ItemsSource="{Binding Customers}" AutoGenerateColumns="False" FlowDirection="RightToLeft" AlternatingRowremoved="AliceBlue" Grid.RowSpan="2" IsSynchronizedWithCurrentItem="True" CanUserResizeColumns="False" CanUserReorderColumns="False" SelectionMode="Single" SelectionUnit="CellOrRowHeader"   >
     <DataGrid.Columns>
         <DataGridCheckBoxColumn Binding="{Binding Path=delete}" Header="deletedata"/>
     </DataGrid.Columns>
  </DataGrid>
  <Button Content="delete" Style="{DynamicResource BlueButtonStyle}" HorizontalAlignment="Left" Foreground="White"  Margin="211,328.2,0,9.8" Grid.Row="2"  Width="118" TextBlock.FontSize="20" Click="OnClicked"/>
</Grid>

behind code:
C#
  private void OnClicked(object sender, RoutedEventArgs e)
    {
//how can get all index rows which user checked. 
    }
Posted

1 solution

You have to use datagridview iteration to search for checked boxes. You should initiate this in your button click event handler. First we have to get all the rows of the datagrid :

C#
public IEnumerable<Microsoft.Windows.Controls.DataGridRow> GetDataGridRows(Microsoft.Windows.Controls.DataGrid grid)
{
    var itemsSource = grid.ItemsSource as IEnumerable;
    if (null == itemsSource) yield return null;
    foreach (var item in itemsSource)
    {
        var row = grid.ItemContainerGenerator.ContainerFromItem(item) as Microsoft.Windows.Controls.DataGridRow;
        if (null != row) yield return row;
    }
}

and then iterate through your grid:
C#
var rows= GetDataGridRows(nameofyourdatagrid); 

foreach (DataGridRow r in rows)  
  {  
    DataRowView rv = (DataRowView)r.Item;
    if (r.Checked)
    {
       foreach (DataGridColumn column in nameofyordatagrid.Columns)
       {
           if (column.GetCellContent(r) is TextBlock)
           {
              TextBlock cellContent = column.GetCellContent(r) as TextBlock;
              MessageBox.Show(cellContent.Text);
           }
       }
  } 

Of course, you could also add a similar method to an CellValueChanged Event , to automate the process. Hope this helps :)
 
Share this answer
 
v2
Comments
mohammad.tofi 26-Sep-13 5:14am    
please help me.keyword "Rows" Error 'System.Windows.Controls.DataGrid' does not contain a definition for 'Rows' and no extension method 'Rows'
Rick van Woudenberg 26-Sep-13 5:50am    
I'm sorry. My mistake. I thought you had a DataGridView, but now I see you have a DataGrid. In that case, you need an additional step, which is to get the rows of the datagrid. I update my code.
mohammad.tofi 26-Sep-13 6:01am    
thank you very much.
please can you explain your code: show me error in DataGridRow and DataGrid in your code "IEnumerable<microsoft.windows.controls.datagridrow> GetDataGridRows(Microsoft.Windows.Controls.DataGrid grid) microsoft.windows.controls.datagridrow
Rick van Woudenberg 26-Sep-13 6:05am    
Could you please post the entire error :)
mohammad.tofi 26-Sep-13 6:09am    
Error 1 The type or namespace name 'DataGridRow' does not exist in the namespace 'Microsoft.Windows.Controls' (are you missing an assembly reference?)



Error 2 The type or namespace name 'DataGrid' does not exist in the namespace 'Microsoft.Windows.Controls' (are you missing an assembly reference?)

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