C#清除ListView中的所有项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/435379/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:16:41  来源:igfitidea点击:

C# Clear all items in ListView

c#listview

提问by Martijn

I try to clear my listview but the clear method doesn't work:

我尝试清除我的列表视图,但清除方法不起作用:

myListView.Items.Clear();

This doen't work. When i put a breakpoint at this line, the line is executed, but my listview isn't empty. How come??

这行不通。当我在这一行放置断点时,该行被执行,但我的列表视图不为空。怎么来的??

I fill my listview by setting it's datasource to a datatable.

我通过将它的数据源设置为数据表来填充我的列表视图。

My solution now is to set the datasource to an empty datatable.

我现在的解决方案是将数据源设置为空数据表。

I just wonder why clear don't do the trick?

我只是想知道为什么明确不做伎俩?

I use a master page. Here some code of a content page when a button is pressed. The method SearchTitle fills the ListView.

我使用母版页。这是按下按钮时内容页面的一些代码。SearchTitle 方法填充 ListView。

Relevant code:

相关代码:

        protected void Zoek()
    {
        // Clear listbox
        ListView1.DataSource = new DataTable();
        ListView1.DataBind();

        switch (ddlSearchType.SelectedValue)
        {
            case "Trefwoorden":
                SearchKeyword();
                break;
            case "Titel":
                SearchTitle();
                break;
            case "Inhoud":
                SearchContent();
                break;
        }
    }

Method that fills the ListView

填充ListView的方法

        private void SearchTitle()
    {
        // Make panel visible
        pnlResult.Visible = true;
        pnlKeyword.Visible = false;

        Search Search = new Search(txtSearchFor.Text);
        ListView1.DataSource = Search.SearchTitle();
        ListView1.DataBind();
    }

采纳答案by Tony Basallo

How about

怎么样

DataSource = null;
DataBind();

回答by JaredPar

Try this ...

尝试这个 ...

myListView.DataSource = null;
myListView.Items.Clear();

回答by Treb

My guess is that Clear()causes a Changedevent to be sent, which in turn triggers an automatic update of your listview from the data source. So this is a feature, not a bug ;-)

我的猜测是这Clear()会导致Changed发送一个事件,这反过来会触发从数据源自动更新您的列表视图。所以这是一个功能,而不是一个错误;-)

Have you tried myListView.Clear()instead of myListView.Items.Clear()? Maybe that works better.

你试过myListView.Clear()代替myListView.Items.Clear()吗?也许这样效果更好。

回答by Martin Moser

I would suggest to remove the rows from the underlying DataTable, or if you don't need the datatable anymore, set the datasource to null.

我建议从底层数据表中删除行,或者如果您不再需要数据表,请将数据源设置为空。

回答by Serhat Ozgel

Probably your code works but it is rebound somewhere after you clear it. Make sure that this it not the case. It will be more helpful if you provide some code. Where are you setting your data source? Where are you data binding? Where are you clearing the list?

可能您的代码有效,但在您清除它后它会在某处反弹。确保情况并非如此。如果你提供一些代码会更有帮助。你在哪里设置你的数据源?你在哪里数据绑定?你在哪里清除清单?

回答by Serhat Ozgel

Don't bother with Clear(). Just do this: ListView.DataSource = null; ListView.DataBind();

不要理会Clear()。只需这样做: ListView.DataSource = null; ListView.DataBind();

The key is the databind(); Works everytime for me.

关键是 databind(); 每次都对我有用。

回答by saurabh gupta

The Problem is arising because you are trying to clear the entire list box. Just use listView1.Items.Clear();

出现问题是因为您试图清除整个列表框。只需使用 listView1.Items.Clear();

回答by saurabh gupta

I did a search on this and I am using WPF c#. Just in case you got here too looking for a WPF solution use the following:

我对此进行了搜索,并且正在使用 WPF c#。以防万一您也来到这里寻找 WPF 解决方案,请使用以下内容:

yourlistview.ItemsSource = null;

yourlistview.ItemsSource = null;

回答by Please help me

Just use the clear method is works like a charm. ListView1.Items.Clear() i think if its not working it may be the position whereby you place this code. Also can try nullifying the datasource.

只需使用 clear 方法就像一个魅力。ListView1.Items.Clear() 我认为如果它不起作用,它可能是您放置此代码的位置。也可以尝试使数据源无效。

回答by nisan

Try with this:

试试这个:

myListView.ItemsSource = new List< DictionaryEntry >();