简体   繁体   English

列表框在数据源更改期间清除所有项目

[英]Listbox clears all items during datasource change

When I change the datasource of my Listbox all items are cleared, when loading new data into this Listbox it stays clear. 当我更改列表框的数据源时,所有项目均被清除,将新数据加载到该列表框中时,其内容保持清除。 I have another ListBox right new to this one with the same refresh code but that one refreshes perfectly. 我有一个与此相同的刷新代码的新ListBox,但是它刷新得很完美。

    private void RefreshContacts()
    {
        this.ListContacts.DataSource = null; 
        this.ListContacts.DataSource = this.contacts;
        this.BoxCountContacts.Text = this.ListContacts.Items.Count.ToString();
    }

Anyone have an idea how to fix the listbox from being cleared and bugged? 任何人都有一个想法,如何解决列表框被清除和错误修复?

Here is the full refresh codes of the two listboxes 这是两个列表框的完整刷新代码

    private Contact[] contacts, feed; private Boolean isFeed;

    internal ArcFeed()
    { 
        this.InitializeComponent();
        this.contacts = this.feed = new Contact[0]; 
    }
    private void RefreshForm(Boolean isFeed)
    { 
        if (isFeed)
        { 
            this.RefreshFeed();
        } 
        else 
        { 
            this.RefreshContacts();
        } 
    }
    private void RefreshContacts()
    {
        this.ListContacts.DataSource = null;         
        this.ListContacts.DataSource = this.contacts;
        this.BoxCountContacts.Text = this.ListContacts.Items.Count.ToString();
    }
    private void RefreshFeed()
    {
        this.ListFeed.DataSource = null; 
        this.ListFeed.DataSource = this.feed;
        this.BoxCountFeed.Text = this.ListFeed.Items.Count.ToString();
    }
    private void OpenFile()
    {
        if (this.isFeed)
        { 
            this.BoxFileFeed.Text = this.DialogOpen.FileName;
            this.feed = ArcBuzz.Load(this.DialogOpen.FileName);
        }
        else
        {
            this.BoxFileContacts.Text = this.DialogOpen.FileName;
            this.contacts = ArcBuzz.Load(this.DialogOpen.FileName); 
        }
        this.RefreshForm(this.isFeed);
    }

All code is debugged and follows it's course properly, I don't see any errors, the correct listbox's datasource are set and changed. 所有代码均已调试,并且可以正常执行,我看不到任何错误,正确的列表框的数据源已设置和更改。

Just to make sure, have you checked that there are actually items in the contacts collection? 只是为了确保您是否检查了contacts集合中是否确实有项目?

Also, if you've got two Listboxes, double check that you are using the right Listbox in each of the refresh code sections. 另外,如果您有两个列表框,请仔细检查每个刷新代码部分中是否使用了正确的列表框。

Sounds stupid, but its silly mistakes like this that get overlooked a lot of the time. 听起来很愚蠢,但是这种愚蠢的错误经常被忽略。 With databinding its usually something small like this stopping it working. 使用数据绑定时,通常像这样的小东西会使它停止工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM