Why doesn't BindingList(Of T) have AddRange Member?
Asked Answered
S

2

10

I think the title pretty much captures my question, but a little bit of background follows:

When a form I have loads it adds a couple of thousand (30k odd) objects to a binding list. When my application loads the first time it takes multiple seconds (around 10 or so from memory) for it to loop through the list of objects and add it to the BindingSource using the add function. However, when this happens on subsequent forms with the same code for loading it only takes a second or two.

So my queries would be:
1. Why doesn't BindingList(Of T) have AddRange Member?
2. Would the initial and subsequent adds be quicker with an AddRange function?
3. Any ideas why one version of the code runs slower than identical versions?

Thanks for any help you might be able to provide.

Schoolgirl answered 22/9, 2010 at 6:15 Comment(3)
Is the form being loaded when the application is loaded? Is that possibly the reason for the slowness?Thoracoplasty
Well after a lot of time I have finally tracked down a (not sure if it's the only) issue to the DataGridView DefaultRowTemplate ContextMenuProperty. When this is set, it adds a considerable amount of time when I add items to the underlying BindingSource.Schoolgirl
For future reference I have come across this msdn.microsoft.com/en-us/library/ha5xt0d9.aspx using "Using Shortcut Menus Efficiently". I've learnt this lesson the hard way.Schoolgirl
T
3

I am not sure why no AddRange method is available. You can easily write your own as an extension method:

    /// <summary>
    /// Adds all the data to a binding list
    /// </summary>
    public static void AddRange<T>(this BindingList<T> list, IEnumerable<T> data)
    {
        if (list == null || data == null)
        {
            return;
        }

        foreach (T t in data)
        {
            list.Add(t);
        }
    }
Typewrite answered 10/11, 2015 at 15:18 Comment(1)
Thanks. This is a real time saver.Built
T
0

From the OP:

Well after a lot of time I have finally tracked down a (not sure if it's the only) issue to the DataGridView DefaultRowTemplate ContextMenuProperty. When this is set, it adds a considerable amount of time when I add items to the underlying BindingSource.

For future reference I have come across this msdn.microsoft.com/en-us/library/ha5xt0d9.aspx using "Using Shortcut Menus Efficiently". I've learnt this lesson the hard way.

The MSDN link in the above article recommends against using a shortcut menu in every cell, especially by putting a shortcut menu in the template. Instead, the user should either create a single shortcut menu for the entire control or handle the CellContextMenuStripNeeded or RowContextMenuStripNeeded event.

Trachoma answered 22/9, 2010 at 6:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.