Nivid Dholakia
Nivid Dholakia

Reputation: 5452

WPF DataGrid Hangs UI when Refreshed

I have a datagrid that has several things where it has dropdowns checkboxes and several different templated columns it also has large amount of data. The problem here is i am setting the property that disables cells and also doing a RaisePropertyChange on that propertybounded. Its updated only when i scroll down and load the data beneath. So i decided to send a Message and Refresh the items in grid.

Following code is for refreshing the grid when it recieves a message.

void RefreshGrid(RefreshGridMessage msg)
    {
        switch (msg.MsgType)
        {
            case RefreshGridMessage.MessageType.ExportChanged:

                this.Dispatcher.Invoke
                   (
                   new RefreshDelegate(
                       delegate
                       {
                           myGrid.Visibility = Visibility.Collapsed;
                           myGrid.Items.Refresh();
                           myGrid.Visibility = Visibility.Visible;
                       }
                      ),
                   null
                   );
                break;
        }
    }

The Problem here is it hangs up UI for few seconds and then it works perfectly and i will get the result what I need.

The question here is there any way to stop this hanging and make it work smoothly?

Upvotes: 0

Views: 541

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81253

Are you refreshing the data from another thread other than UI thread. In case you don't want your UI to be supsend for that time period use BeginInvoke instead of Invoke to queue it on Dispatcher. The refresh delegate will be called asynchronously and your UI will remain responsive for that time period.

Upvotes: 1

Related Questions