listbox Refresh() in c#

て烟熏妆下的殇ゞ 提交于 2020-01-10 14:18:39

问题


int[] arr = int[100];
listBox1.DataSource = arr;
void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
{
    .....//some processes
    listBox1.DataSource = null;
    listBox1.DataSource = arr;
}

is not working,

also,

listBox1.Refresh(); is not working,

also,

listBox1.Update(); is not working,


i know i can use BindingList<T> but i have to work with only array.

can you help me how can i refresh listbox?


回答1:


try the following

listBox1.DataBind()



回答2:


my first answer on stack exchange here.

C# .Net 4.0:

listBox1.DataSource = null;
listBox1.DataSource = names;

I noticed that setting the datasource for the first time, it refreshes. When it's set, and you try set it to the same one again, it doesn't update.

So I made it null, set it to the same one, and it displayed correctly for me with this issue.




回答3:


Managed to do just with

FirstListBox.DataContext = null;
FirstListBox.DataContext = App.ViewModel;

Simply loses link and get all the data back to it.




回答4:


The problem might come from the ListBox SelectionMode.

For a reason that I don't know, the databinding does not work when SelectionMode is SelectionMode.None.

A workaround could be:

listBox.SelectionMode = SelectionMode.MultiExtended;
listBox.DataSource = myDatasource;
listBox.SelectionMode = SelectionMode.None;

Hope it helps.




回答5:


ListBox only updates its shown content when the object that is binded on dataSource notifys it own changes. the BindingSource object has an event called DataSourceChanged. when the Source is changed to a different object the Listbox will update itself. Same thing when you bind a List. Nothing will happen if you change the List, because the List doesn't notify that it has been changed. There is a Simple solution for this Problem: use BindingList http://msdn.microsoft.com/de-de/library/ms132679%28v=vs.110%29.aspx

the BindingList has the ListChanged Event is called every time when the List is changed (obviously). So the DataBindings of Windows.Form objects use events like ListChanged to update themselves. A simple List doesn't support this event.

SO if you want to work with a lot of Data Bindings you should know about: http://msdn.microsoft.com/de-de/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx




回答6:


well, without binding I only managed with:

this.Hide();
this.Show();

it redraws everything...




回答7:


I inherited ListBox and added a public method calling RefreshItems() which does what we want. Already implemented and all. I dont know why they didnt put in a public method.




回答8:


Use BeginUpdate and EndUpdate, that should solve it. No need to set the data source twice

listBox1.BeginUpdate();

listBox1.DataSource = myList;

listBox1.EndUpdate();



回答9:


These links might help.

How can I update a listbox item (C#)? - http://arstechnica.com/civis/viewtopic.php?f=20&t=554717

Bind ArrayList to ListBox - http://www.java2s.com/Tutorial/CSharp/0460__GUI-Windows-Forms/BindArrayListtoListBox.htm




回答10:


Windows forms to see changes especially on Listbox and other controls before load is finished is tricky. To see data as its loaded use invalidate(); then Update();



来源:https://stackoverflow.com/questions/5136418/listbox-refresh-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!