C# 清除wpf中的datagrid值
每次单击C# 清除wpf中的datagrid值,c#,.net,wpf,datagrid,C#,.net,Wpf,Datagrid,每次单击treevieItem时,我都需要刷新我的datagrid。我的代码如下 private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) { this.dataGrid1.Columns.Clear(); this.dataGrid1.ItemsSource= null; String path =this.treeV
treevieItem
时,我都需要刷新我的datagrid
。我的代码如下
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;
}
}
private void treeView1\u SelectedItemChanged(对象发送方,RoutedPropertyChangedEventArgs e)
{
this.dataGrid1.Columns.Clear();
this.dataGrid1.ItemsSource=null;
字符串路径=this.treeView1.SelectedItem;
如果(!File.Exists(path))
MessageBox.Show(“未找到”);
其他的
{
ob.provider(路径);
//字符串数据=@“C:\logs.xml”;
字符串数据=路径;
对象引用功能(数据);
this.dataGrid1.ItemsSource=objref.Result;
}
}
但每次我单击treeview项datagrid时,它都不会被清除,而是附加了传入的数据。
我同时使用了dataGrid1.Columns.Clear()
和dataGrid.ItemSource=null代码>
如果您使用以下方法填充DataGrid,我该怎么做
dataGrid.Items.Add(someObject);
那么您应该能够使用:
dataGrid.Items.Clear();
删除所有行
如果要绑定到ItemsSource,请执行以下操作:
dataGrid.ItemsSource = someCollection;
然后您应该能够将ItemsSource设置为null,它将删除所有行
编辑:
别忘了刷新它:
dataGrid.Items.Refresh();
我有一个公共的IEnumerable
集合,每次调用函数时都会追加该集合。所以,通过重写,我刷新了我的DATAGRID中的数据。 < P>您可以考虑使用<代码> StababCeleCopy< /Cord>类,而不是<代码> iQueaby.P/>
我尝试过几种方法,这是迄今为止最好、最可靠的方法:
dataGrid.Columns.Clear();
dataGrid.Items.Clear();
dataGrid.Items.Refresh();
我可以通过将DataContext设置为null来清除数据网格
DataGrid.DataContext=null;
如果它绑定了Itemsource,最简单的方法是
dataGrid1.ItemSource=null代码>在清除项目之前,必须解除项目源的绑定
MyGrid.ItemsSource = null;**//un-bind the itemsource first**
MyGrid.Items.Clear();**//**
我正在填充ItemsSource。我已经将其设置为空。这是我的问题。但是我的datagrid仍然没有清除..这是您填充它的唯一位置吗?请尝试刷新它:datagrid.Items.Refresh();
DataGrid.DataContext=null;
MyGrid.ItemsSource = null;**//un-bind the itemsource first**
MyGrid.Items.Clear();**//**