Results 1 to 7 of 7

Thread: ListBox Refresh DataSource?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: ListBox Refresh DataSource?

    Quote Originally Posted by jmcilhinney View Post
    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: ListBox Refresh DataSource?

    Quote Originally Posted by jmcilhinney View Post
    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?

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    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?

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    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;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width