如何刷新ListBox的项目? [英] How refresh items of a ListBox?

查看:80
本文介绍了如何刷新ListBox的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
C#强制ListBox更新元素

Possible Duplicate:
C# Force ListBox to update elements

让我们考虑一下这小段代码:

Let's consider this small piece of code:

listbox.DataSource = base_items;
listbox.DisplayMember = "Name";
// ... a bit later in the program
base_items.Add(  new Base_Item("Unnamed")  );

从这一点来看,如何使列表框更新其项目?我看到更新的唯一方法是关闭窗口,然后重新加载.

From this point, how do I do to make the listbox update its items? The only way for me to see the update is to close the window and reload it again.

推荐答案

只需删除并添加数据绑定即可. 您可以创建可以在首次加载时以及添加新项目时使用的方法:

Just remove and add databinding again. You can create method that can be used on first load and when new item was added:

    void BindData()
    {
        listBox.DataSource = null;
        listBox.DataSource = base_items;
        listbox.DisplayMember = "Name";
    }

这是添加新项和刷新列表框的代码:

So here is the code for adding new item and refreshing listbox:

    base_items.Add(new Base_Item("Unnamed"));
    BindData();

这篇关于如何刷新ListBox的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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