ListBox已设置数据源,但Refresh不执行任何操作 [英] ListBox has set datasource but Refresh does nothing

查看:610
本文介绍了ListBox已设置数据源,但Refresh不执行任何操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎误解ListBox.Refresh()方法,我希望有人能帮助我。

I seem to be misunderstanding the ListBox.Refresh() method and I was hoping someone could help me out.

我想做什么:

我想在窗体初始化时加载列表框的数据(source = sql数据库的表)。此外,当用户向数据库添加数据时,我想更新列表框。

What I am trying to do:
I want to load a listbox's data (source = table of a sql database) upon initialization of a windows form. Also when the user adds data to the database I would like the listbox to update.

逻辑:

我有一个sql数据库作为我的源,它设置为:

Logic:
I have a sql database as my source, it is set as:

listBoxDays.DataSource = DBQuery.informationRetreval().DefaultView;

DBquery.informationRetreval()是我的DBQuery静态类中的一个静态方法。它所做的是从数据库设置一个表,然后返回表。

DBquery.informationRetreval() is a static method within the my DBQuery static class. All it does is set up a table from the database and then returns the table.

我设置的数据源与initializeComponent相同的方法(所以我的列表框将加载现有数据):

I set the datasource within the same method as the initializeComponent (so my listbox will load with the existing data):

public Settings()
    {
        InitializeComponent();
        listBoxDays.DataSource = DBQuery.informationRetreval().DefaultView;
    }

当用户添加更多数据时:我调用一个方法,数据到数据库,然后我调用:

When the user adds more data: I call a method in which I add the data to the database and then I call:

    listBoxDays.Refresh(); //update listbox

问题:
这不更新列表框。在初始化我的列表框的数据将填充,但后不会改变(因此刷新不工作)。这是为什么?我可以再次设置DataSource,但感觉马虎。在查看一些文档后,我注意到事件处理程序DataSourceChanged这可能更多的是我正在寻找。但是为什么不刷新工作?

The Problem: This does not update the listbox. Upon initialization of my listbox the data will populate but after it will not change (hence refresh does not work). Why is that? I could set the DataSource again but that feels sloppy. After looking into some of the documentation I noticed the event handler DataSourceChanged which maybe more for what I am looking for. Nevertheless why wouldn't refresh work?

感谢您的耐心等待。

推荐答案


刷新():强制控制以使其客户区域无效并立即
重新绘制自身和任何子控件。

Refresh(): Forces the control to invalidate its client area and immediately redraw itself and any child controls.

刷新不会重新绑定您的控件将只会导致控件重绘。您将必须再次使用

Refresh will not rebind your control, it will just cause the control to be redrawn. You will have to set the DataSource again with

listBoxDays.DataSource = DBQuery.informationRetreval().DefaultView;

并重新绑定。

资料来源: Microsoft MSDN

这篇关于ListBox已设置数据源,但Refresh不执行任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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