清除wpf中的datagrid值 [英] Clear datagrid values in wpf

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

问题描述

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

但是,每次点击树状视图项目时,datagrid不是已清除 - 它附加了传入的数据。
我同时使用 dataGrid1.Columns.Clear() dataGrid.ItemSource = null;
如何使用以下方式填充DataGrid?

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.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中的datagrid值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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