I need to flush my datagrid
everytime when a treeviewitem
is clicked. My code is given below.
private void treeView1_SelectedItemCh
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();
You have to unbind the itemsource before you clear the item
MyGrid.ItemsSource = null;**//un-bind the itemsource first**
MyGrid.Items.Clear();**//**
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();
I had a public IEnumerable
collection which is appended every time the function is called. So by overwriting it, I flushed the Data in my Datagrid.
If it is bound an Itemsource, the simplest way is
dataGrid1.ItemSource = null;