Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid is very slow to render

I have tried using both a customized DataGrid as well as the stock one in WPF. I have tried populating them manually as well as through bindings. In both cases they are slow.

I have a scenerio where the user clicks on a button and a DataGrid appears with appropriate data. Currently I am in proof of concept mode and just using sample data. I have a DataSet with a table that has 10 rows in it.

If I don't attach any data to the DataGrid when I click the button the empty DataGrid displays pretty much instantly, a user cannot perceive a delay. As soon as I add 10 rows of data, for 6 columns, the delay is about 2 seconds, very noticable to the user.

I even tried filling with empty data, just to get an empty grid to appear and it is equally as slow.

for (int i = 0; i < 10; i++)     _dataGrid.Items.Add(""); 

I put a timer to count the ticks from when the button is clicked to when all of the code is executed to draw the DataGrid and it is around 20 milliseconds, so the code executes very fast, but on the screen is where the big lag is. I tried a GridView and it renders much fast on the screen.

I have heard various reports of slow DataGrid drawing with complex scenarios and using 1000's of rows, but this is as simple as it gets, 6 columns by 10 rows filled with empty data.

For readonly display is GridView an equally viable option to the DataGrid?


Update

Here is the creation of my columns.

                DataGridTextColumn column = new DataGridTextColumn();                 column.ColumnWidthChanged += new ColumnWidthChangedEventHandler(column_ColumnWidthChanged);                  column.Header = entity.GetPropertyValue("ColumnLabel");                 column.Binding = new Binding(entity.GetPropertyValue("Tag"));                 column.Width = new DataGridLength(entity.GetPropertyDouble("DisplaySize"));                 _dataGrid.Columns.Add(column); 

This is a how I bind the DataSet with 10 rows in it.

                _dataGrid.ItemsSource = ds.Tables[0].DefaultView;                 _dataGrid.DataContext = ds.Tables[0]; 

Not sure what I can do differently.

like image 932
David Gunther Avatar asked Jul 13 '11 14:07

David Gunther


2 Answers

Are you have:

  • Enabled VirtualizingStackPanel.VirtualizationMode for a Grid? if not - try to set.
  • Set VirtualizingStackPanel.IsVirtualizing="true" for DataGrid
  • Wrapped up a Grid by a StackPanel container? If yes - try to remove.
  • Wrapped up a Grid by an external ScrollViewer control? If yes - try to remove.

One more point, could you bind whole items collection at once instead of adding each item into the grid.Items collection?

like image 148
sll Avatar answered Sep 19 '22 06:09

sll


A general tip for DataGrid performance issues: I had a problem with the DataGrid in which it took literally seconds to refresh after a window resize, column sort, etc. and locked up the window UI while it was doing so (1000 rows, 5 columns).

It came down to an issue (bug?) with the WPF sizing calculations. I had it in a grid with the RowDefinition Height="Auto" which was causing the rendering system to try and recalculate the size of the DataGrid at runtime by measuring the size of each and every column and row, presumably by filling the whole grid (as I understand it). It is supposed to handle this intelligently somehow but in this case it was not.

A quick check to see if this is a related problem is to set the Height and Width properties of the DataGrid to a fixed size for the duration of the test, and try running again. If your performance is restored, a permanent fix may be among these options:

  • Change the sizes of the containing elements to be relative (*) or fixed values
  • Set MaxHeight and MaxWidth of the DataGrid to a fixed value larger than it could get in normal use
  • Try another container type with different resizing strategy (Grid, DockPanel, etc)
like image 27
TripleAntigen Avatar answered Sep 21 '22 06:09

TripleAntigen