0

I have a method in a DispatcherTimer that refreshes my datagrid's data every 3 seconds. (3 seconds for testing purposes)

The thing is that every time my datagrid gets refreshed, it clears all the data and then loads the data again from my database(obviously). Now my issue is that it creates this "flashy" effect each time the data loads, because of all the rows being cleared and the re-loaded into the datagrid.

So my question is, (after you have taken a look at my code below) is there a better way of loading the data again and again without creating the flashing effect in my datagrid?

My method where I call my data from my WCF service:

public async Task LoadTrucks()
{
    using (TruckServiceClient service = new TruckServiceClient())
    {
        List<ClientItems> truckitems = new List<ClientItems>();
        if (dgViewProjects.Items.Count <= 0)
        {
            foreach (var item in await service.GetTrucksAsync())
            {
                truckitems.Add(new ClientItems
                {
                    TruckQuoteId = item.QuoteId,
                    TruckChassisManufacturer = item.ChassisManufacturer,
                    TruckChassisModel = item.ChassisModel,
                    TruckStatus = item.Status,
                    TruckJobNumber = item.JobNumbers,
                    TruckAddedBy = item.AddedBy,
                    TruckClient = item.ClientName
                });
            }
        }
        dgViewProjects.ItemsSource = (truckitems.ToArray());
    }
}

Here I call the LoadTrucks() method and create my DispatcherTimer:

private async void dgViewProjects_Loaded(object sender, RoutedEventArgs e)
{
    await LoadTrucks();

    var timer = new DispatcherTimer();
    timer.Interval = TimeSpan.FromSeconds(3);
    timer.Tick += new EventHandler(async (object o, EventArgs t) =>
    {
        dgViewProjects.ItemsSource = null;
        await LoadTrucks();
    });
    timer.Start();
}
CareTaker22
  • 1,260
  • 3
  • 18
  • 36
  • Try the answers [here](http://stackoverflow.com/a/2041839/4875631) or [here](http://stackoverflow.com/questions/19358499/my-datagridview-blinks-when-refreshing) – Blue Jul 25 '16 at 17:21
  • Both of those links are for WinForms and not WPF :( – CareTaker22 Jul 25 '16 at 17:34
  • Could you not tailor that for WPF? Is there not a way to suspend the layout on WPF? – Blue Jul 25 '16 at 17:34
  • As far as I have read, in WinForms you can use DoubleBuffering to help with the flickering issue and WPF uses DirectX to render the UI(it double buffers automatically), so I do not see a solution at the moment. Still searching... – CareTaker22 Jul 25 '16 at 17:36
  • Create a property of type CollectionView on top of truckItems like TruckItemsCollection = CollectionViewSource.GetDefaultView(truckItems) as CollectionView; then bind your DataGrid to this TruckItemsCollection in XAML. Then use CollectionView.DeferRefresh to draw the grid after all the long running routines are over. – Alex Zhmerik Jul 25 '16 at 20:28

2 Answers2

1

Check if the item with a given key already exists and update its properties instead of completely switching out all items.

(This also is a good idea in terms of performance, your current logic requires a complete recreation of the data grid contents.)

H.B.
  • 166,899
  • 29
  • 327
  • 400
1

In the real application are you getting data in a streaming fashion from something like a Queuing service?

I ask because instead of replacing all the data can't you just update/insert instead to avoid this?

If not you can try something like this: In WPF, what is the equivalent of Suspend/ResumeLayout() and BackgroundWorker() from Windows Forms

Community
  • 1
  • 1
Kelly
  • 6,992
  • 12
  • 59
  • 76