ListBox Refresh DataSource?
I want to refresh my list because right now I have a custom List with a sub List and I want to have each sub list entry as a separate line.
Code:
class WDFFileList : List<WDFFile>, System.ComponentModel.IListSource
{
public bool ContainsListCollection
{
get
{
return true;
}
}
public System.Collections.IList GetList()
{
List<WDFEntry> entries = new List<WDFEntry>();
foreach (WDFFile f in this)
{
foreach (WDFEntry e in f.Entries)
{
entries.Add(e);
}
}
return entries;
}
}
Now this "works" as in when I first set the databind GetList() is called. But how do I refresh the datasource? Like when I add some entries I want to update the listbox.
I have tried
Code:
UsageList.DataSource = null;
UsageList.DataSource = Files;
But GetList() wasn't called.
Just tried this
Code:
class WDFFileList : List<WDFFile>, System.ComponentModel.IListSource
{
public List<WDFEntry> Entries = new List<WDFEntry>();
public void Refresh()
{
Entries.Clear();
foreach (WDFFile f in this)
{
foreach (WDFEntry e in f.Entries)
{
Entries.Add(e);
}
}
}
public bool ContainsListCollection
{
get
{
return true;
}
}
public System.Collections.IList GetList()
{
return Entries;
}
}
Still didn't update.
How do you update a custom datasource? Or am I approaching this wrong?
Re: ListBox Refresh DataSource?
You should inherit BindingList instead of List and then you can call ResetBindings, although I've never tried it with a custom IListSource. The problem I see is that your class is an IList AND and IListSource, which may be causing its IListSource nature to be ignored and its IList nature used. Generally a class won't implement both. To avoid this potential conflict you might want to hide your List inside rather than inheriting, so that you class will only implement IListSource.
Re: ListBox Refresh DataSource?
Quote:
Originally Posted by
jmcilhinney
You should inherit BindingList instead of List and then you can call ResetBindings, although I've never tried it with a custom IListSource. The problem I see is that your class is an IList AND and IListSource, which may be causing its IListSource nature to be ignored and its IList nature used. Generally a class won't implement both. To avoid this potential conflict you might want to hide your List inside rather than inheriting, so that you class will only implement IListSource.
I will try BindingList.
I tried that, it gave the same result. GetList() was only called once when I set the DataSource.
Re: ListBox Refresh DataSource?
So try the rest of my advice. Implementing IList and IListSource in the same class is asking for trouble because an IListSource is supposed to be the source of a list, not a list itself.
Re: ListBox Refresh DataSource?
Quote:
Originally Posted by
jmcilhinney
So try the rest of my advice. Implementing IList and IListSource in the same class is asking for trouble because an IListSource is supposed to be the source of a list, not a list itself.
But with BindingSource can you give the ListBox a custom list?
Re: ListBox Refresh DataSource?
Code:
class WDFEntryList : System.ComponentModel.BindingList<WDFEntry>
{
}
class WDFFileList : List<WDFFile>
{
public WDFEntryList Entries = new WDFEntryList();
public void Refresh()
{
Entries.Clear();
foreach (WDFFile f in this)
{
foreach (WDFEntry e in f.Entries)
{
Entries.Add(e);
}
}
}
}
It "works" but it takes an awful long time to load because I think it is rendering each time it adds an item. Is there a way to pause the rendering?
Re: ListBox Refresh DataSource?
Okay well this works and doesn't take 10-20 seconds to load.
Code:
class WDFEntryList : List<WDFEntry>, System.ComponentModel.IListSource
{
public bool ContainsListCollection
{
get { return true; }
}
public System.Collections.IList GetList()
{
return this;
}
}
class WDFFileList : List<WDFFile>
{
public WDFEntryList Entries = new WDFEntryList();
public void Refresh()
{
Entries.Clear();
foreach (WDFFile f in this)
{
foreach (WDFEntry e in f.Entries)
{
Entries.Add(e);
}
}
}
}
Code:
UsageList.DataSource = null;
Files.Refresh();
UsageList.DataSource = Files.Entries;