Search code examples
wpfdatagridrows

clearing datagrid Itemssource in wpf


I'm using a datagrid in my program,and bind it with Itemssource. I wanna clear all rows programmatically ,not one by one and I wanna save my Itemssource so I can't use datagrid.Itemssource=null; because I can't use datagrid with that Itemssource again, and I dont want to make a new Itemssource each time,in fact,I have to clear my Itemssource programmatically. now,any idea to clear rows?? thx


Solution

  • if you have a ObservableCollection bind to your DataGrid ItemsSource simply use .Clear()

    public OberservableCollection<MyItem> MyCollection {get;set;}
    
    <DataGrid ItemsSource="{Binding MyCollection}" />
    
    
    //anywhere
    MyCollection.Clear();