C#清除ListView中的所有项

标签 c# listview

我尝试清除我的 ListView ,但清除方法不起作用:

myListView.Items.Clear();

这行不通。当我在这一行放置一个断点时,该行被执行,但我的 ListView 不为空。怎么会??

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

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

我只是想知道为什么 clear 不这样做?

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

相关代码:

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

填充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();
    }

最佳答案

怎么样

DataSource = null;
DataBind();

关于C#清除ListView中的所有项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/435379/

相关文章:

c# - 对 WCF 服务的 POST 调用出现问题 - 400 BAD REQUEST

c# - 使用 NHibernate 的简单获取(使用按代码映射)非常慢

c# - 网页中的二维码阅读器

android listview OpenGLRenderer 和纹理问题?

android - 什么取代了 WearableListView.Adapter?

android - ListView 重复数据

java - 如何通过序列化在 Intent 之间传递自定义 ArrayAdapter 对象

c# - 在 DataGrid wpf 中创建自定义编辑行

java - BaseAdapter 和 Picasso 问题

c# - WPF 绑定(bind) : Where a property contains the path to the value