Search code examples
wpfwpfdatagrid

WPF DG- How to delete selected row on Button click event from DataGrid


I have the following problem.

I select a row from my DataGrid with mouse click like this: Admin admin=(Admin)dGrid.SelectedItem;

How can I remove this row with button click event? I couldn't use Rows.Delete and didn't find any delete,remove commands in WPF. Thanks for your help!


Solution

  • If you are binding your DataGrid to an observable collection (in two ways mode) then removing the selected item from the collection is all what you need to do; here is an example of how to proceed (DoubleClick To delete):

     <Grid>     
        <DataGrid ItemsSource="{Binding AdminCollection,Mode=TwoWay}" SelectedItem="{Binding SelectedAdmin}" MouseDoubleClick="DeleteRowEvent" >           
        </DataGrid>        
    </Grid>
    

    and the corresponding code behind:

     public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private Admin _selectedAdmin = new Admin();
        public Admin SelectedAdmin
        {
            get { return _selectedAdmin; }
            set
            {
                if (_selectedAdmin == value) return;
                _selectedAdmin = value;
                OnPropertyChanged();
            }
        }
    
        private ObservableCollection<Admin> _adminCollection = new ObservableCollection<Admin>();
        public ObservableCollection<Admin> AdminCollection
        {
            get { return _adminCollection; }
            set
            {
                if (_adminCollection == value) return;
                _adminCollection = value;
                OnPropertyChanged();
            }
        }
        public MainWindow()
        {
            InitializeComponent();
            AdminCollection = new ObservableCollection<Admin>()
        {
            new Admin()
            {
                Name = "James",Age = 34, Location = "Paris"
            },
             new Admin()
            {
                Name = "Joe",Age = 34, Location = "Us"
            },
             new Admin()
            {
                Name = "Ann",Age = 34, Location = "Canada"
            },
        };
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    
        private void DeleteRowEvent(object sender, MouseButtonEventArgs e)
        {
            AdminCollection.Remove(AdminCollection.First(x => x.Id == SelectedAdmin.Id));
        }
    }
    public class Admin
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
        public int Age { get; set; }
    }
    

    the DataContext is set to the code behind in the MainWindow.xaml

    DataContext="{Binding RelativeSource={RelativeSource Self}}"