清除 wpf 中的数据网格值 [英] Clear datagrid values in wpf

查看:17
本文介绍了清除 wpf 中的数据网格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次点击 treeviewitem 时,我都需要刷新我的 datagrid.我的代码如下.

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;
    }
}

但是每次当我单击树视图项时,数据网格都不会被清除——它附加了传入的数据.我同时使用了 dataGrid1.Columns.Clear()dataGrid.ItemSource= null;我该怎么做??

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??

推荐答案

如果您使用以下方法填充 DataGrid:

If you are populating the DataGrid by using:

dataGrid.Items.Add(someObject);

那么你应该可以使用:

dataGrid.Items.Clear(); 

删除所有行.

如果您绑定到 ItemsSource,例如:

If you are binding to the ItemsSource like:

dataGrid.ItemsSource = someCollection;

然后您应该能够将 ItemsSource 设置为 null,它将删除所有行.

Then you should be able to set the ItemsSource to null and it will remove all the rows.

不要忘记刷新它:

dataGrid.Items.Refresh();

这篇关于清除 wpf 中的数据网格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆