简体   繁体   中英

DataGrid.SelectedItems not recongnised

I am using a datagrid to display data from a database, I need to get the selected row, but when i use DataGrid.SelectedItem, it does not recognize the property SelectedItem.Does any one know what the reason might be? I use bindinglist to bind the data to the grid.

    private void delete_btn_Click(object sender, RoutedEventArgs e)
    {
        if (selectAll == true)
        {
            ThisAddIn.sourceContext.removeAll();
        }
        else
        {                
            var grid = sender as DataGrid;
            var selected = grid.SelectedItems;

            foreach (var item in selected)
            {
                ThisAddIn.sourceContext.removeEntity ((Source)item);
            }                
        }           
    }

        DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}" 
        ItemsSource="{Binding ObservableEvtCode}" 
        Name="SourceDataGrid" AutoGenerateColumns="True" Margin="10,40,10,10" IsReadOnly="True" 

SelectedItem is part of the DataGrid class.

However, if nothing is selected, then you run into issues.

Does this help?

      var selected = grid.SelectedItems;
      if (selected != null)
      {
        foreach (var item in selected)
        {
            ThisAddIn.sourceContext.removeEntity ((Source)item);
        }
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM