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();
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.
我能够通过将 DataContext 设置为 null 来清除我的数据网格。
DataGrid.DataContext=null;
如果绑定了一个Itemsource,最简单的方法就是
dataGrid1.ItemSource = null;
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??
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.