tags:

views:

540

answers:

2

For a personal project, I need to dynamically populate a grid, based on the contents of an array of variable size. I use code along the lines of what is below to do that, and it works well, except that when the array grows large (as in 200 x 200 or more) it becomes slow (20+ secs to populate). It looks like instantiating the buttons is fast, but adding to the grid is slow.
Am I doing anything wrong? Is there anything I could do to speed up the process using the regular WPF grid? Should I look at another control? Thanks in advance for any suggestions.

        int columns=200;
        int rows=200;

        var width = new GridLength(30);
        var height = new GridLength(25);

        for (int column = 0; column < columns; column++)
        {
            var columnDefinition = new ColumnDefinition();
            columnDefinition.Width = width;
            this.TestGrid.ColumnDefinitions.Add(columnDefinition);
        }

        for (int row = 0; row < rows; row++)
        {
            var rowDefinition = new RowDefinition();
            rowDefinition.Height = height;
            this.TestGrid.RowDefinitions.Add(rowDefinition);
        }

        for (int column = 0; column < columns; column++)
        {
            for (int row = 0; row < rows; row++)
            {
                var button = new Button();
                button.Content = row.ToString() + ", " + column.ToString();
                Grid.SetRow(button, row);
                Grid.SetColumn(button, column);
                this.TestGrid.Children.Add(button);
            }
        }
A: 

Have you tried surrounding your loop in:

GridView.BeginUpdate();

// add items

GridView.EndUpdate();
Nick Bedford
The `BeginUpdate` will stop the control updating/refreshing until the `EndUpdate` is hit, thus saving significant amounts of time.
ChrisF
What's that? I can't find anything similar in standard WPF GridView control. Even if it is there - original question was about Grid, not GridView. Mathias try to update grid while it's detached from the visual tree.
Anvaka
That makes sense, but... the Grid control does not seem to have either BeginUpdate() and EndUpdate(). Am I missing something?
Mathias
Sorry, my mistake.
Nick Bedford
@Anvaka: can you please elaborate? I tried to create a Grid control in code, add the buttons, then add the Grid to the Window, but it doesn't seem to make a difference. Is this along the lines of what you meant?
Mathias
+1  A: 

Admittedly I'm still getting my chops wet with WPF, but I'm going to go out on a limb here and say that trying to add 40,000 controls is your real bottleneck; not so much as to how you're adding the controls.

Even if you had all 40,000 controls hard coded in your XAML, you'd still end up with a 20+ second load time.

Either this is the world's largest data entry form or a massive Mine Sweeper board ;-)

Metro Smurf
That's what I fear, too, but I was hoping maybe there was a magic trick I didn't know about... And I am not building the World Championship Mine Sweeper board, but essentially replicating the Excel UI.
Mathias
Thank you a million! Your answer got me to rethink about the issue, and realize that I can completely avoid the issue by changing my design.
Mathias
Also make sure you take a look at Row and Column Virtualization. This is all about keeping the number of created WPF elements low even when you bind to a lot of elements.
Dave Markle