I need to flush my datagrid everytime when a treeviewitem is clicked. My code is given below.
private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    this.dataGrid1.Columns.Clear();
    this.dataGrid1.ItemsSource= null;
    String path =this.treeView1.SelectedItem;
    if (!File.Exists(path))
        MessageBox.Show("Not Found");
    else
    {
        ob.provider(path);
        //   String data = @"C:\logs.xml";
        string data = path;
        objref.functionality(data);
        this.dataGrid1.ItemsSource = objref.Result;
    }
}
But everytime when I click a treeview item datagrid is not cleared-- it's appended with incoming data. 
I used both dataGrid1.Columns.Clear() and dataGrid.ItemSource= null;
How can i do this??
If you are populating the DataGrid by using:
dataGrid.Items.Add(someObject);
Then you should be able to use:
dataGrid.Items.Clear(); 
To remove all the rows.
If you are binding to the ItemsSource like:
dataGrid.ItemsSource = someCollection;
Then you should be able to set the ItemsSource to null and it will remove all the rows.
EDIT:
Don't forget to refresh it:
dataGrid.Items.Refresh();
                        You may consider using ObservableCollection<> class rather than IEnumerable<>.
ObservableCollection<User> users = new ObservableCollection<User>();
dataGrid1.ItemsSource = users;
You can clear the datagrid by using the below code.
users.Clear();
                        I have tried several approaches and this was by far the best and most reliable one:
dataGrid.Columns.Clear();
dataGrid.Items.Clear();
dataGrid.Items.Refresh();
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With