C# ListBox 已设置数据源但刷新什么也不做
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14969650/
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
ListBox has set datasource but Refresh does nothing
提问by S1r-Lanzelot
I seem to be misunderstanding the ListBox.Refresh() method and I was hoping someone could help me out.
我似乎误解了 ListBox.Refresh() 方法,我希望有人可以帮助我。
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.
我想要做什么:
我想在 Windows 窗体初始化时加载列表框的数据(源 = sql 数据库的表)。此外,当用户将数据添加到数据库时,我希望列表框更新。
Logic:
I have a sql database as my source, it is set as:
逻辑:
我有一个 sql 数据库作为我的源,它被设置为:
listBoxDays.DataSource = DBQuery.informationRetreval().DefaultView;
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.
DBquery.informationRetreval() 是我的 DBQuery 静态类中的一个静态方法。它所做的只是从数据库中设置一个表,然后返回该表。
I set the datasource within the same method as the initializeComponent (so my listbox will load with the existing data):
我在与 initializeComponent 相同的方法中设置数据源(因此我的列表框将加载现有数据):
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
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?
问题:这不会更新列表框。在我的列表框初始化时,数据将填充,但之后不会改变(因此刷新不起作用)。这是为什么?我可以再次设置数据源,但这感觉很草率。在查看了一些文档后,我注意到事件处理程序 DataSourceChanged 可能更适合我正在寻找的内容。然而为什么不能刷新工作?
Thank you for your patience. Please let me know if I need to be more clear.
感谢您的耐心等待。如果我需要更清楚,请告诉我。
采纳答案by Hanlet Esca?o
Refresh(): Forces the control to invalidate its client area and immediately redraw itself and any child controls.
Refresh():强制控件使其客户区无效并立即重绘自身和任何子控件。
Refresh will not rebind your control, it will just cause the control to be redrawn. You will have to set the DataSource
again with
刷新不会重新绑定您的控件,它只会导致重新绘制控件。你将不得不DataSource
再次设置
listBoxDays.DataSource = DBQuery.informationRetreval().DefaultView;
and re-bind it.
并重新绑定。
Source: Microsoft MSDN
来源:微软 MSDN
回答by RandomUs1r
It'll only refresh when objects have changed (ex. a different query), not the data. You can do this:
它只会在对象发生变化(例如不同的查询)时刷新,而不是数据。你可以这样做:
listBoxDays.DataSource = null;
listBoxDays.DataSource = DBQuery.informationRetreval().DefaultView;
to refresh it... everytime you need to.
刷新它...每次你需要。