C# 清除 wpf 中的数据网格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14472894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear datagrid values in wpf
提问by BinaryMee
I need to flush my datagrid
everytime when a treeviewitem
is clicked. My code is given below.
datagrid
每次treeviewitem
点击a 时,我都需要刷新我的。我的代码如下。
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??
但是每次当我单击树视图项时,数据网格都不会被清除——它附加了传入的数据。我同时使用了dataGrid1.Columns.Clear()
和dataGrid.ItemSource= null;
我该怎么做??
回答by Rhexis
If you are populating the DataGrid by using:
如果您使用以下方法填充 DataGrid:
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:
如果您绑定到 ItemsSource,例如:
dataGrid.ItemsSource = someCollection;
Then you should be able to set the ItemsSource to null and it will remove all the rows.
然后您应该能够将 ItemsSource 设置为 null,它将删除所有行。
EDIT:
编辑:
Don't forget to refresh it:
不要忘记刷新它:
dataGrid.Items.Refresh();
回答by BinaryMee
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.
我有一个公共IEnumerable
集合,每次调用该函数时都会附加该集合。因此,通过覆盖它,我刷新了 Datagrid 中的数据。
回答by klaydze
You may consider using ObservableCollection<>
class rather than IEnumerable<>
.
您可以考虑使用ObservableCollection<>
class 而不是IEnumerable<>
.
ObservableCollection<User> users = new ObservableCollection<User>();
dataGrid1.ItemsSource = users;
You can clear the datagrid by using the below code.
您可以使用以下代码清除数据网格。
users.Clear();
回答by polfosol ?_?
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();
回答by user1579234
I was able to clear my Data-Grid by setting the DataContext to null.
我能够通过将 DataContext 设置为 null 来清除我的数据网格。
DataGrid.DataContext=null;
回答by Shreyas
If it is bound an Itemsource, the simplest way is
如果绑定了一个Itemsource,最简单的方法就是
dataGrid1.ItemSource = null;
dataGrid1.ItemSource = null;
回答by OOP Core Concepts
You have to unbind the itemsource before you clear the item
您必须在清除项目之前解除绑定项目源
MyGrid.ItemsSource = null;**//un-bind the itemsource first**
MyGrid.Items.Clear();**//**