Bogdan Verbenets
Bogdan Verbenets

Reputation: 26936

need help optimizing performance of a datagrid

I have a DataGrid with 3000 rows and 12 columns. DataGrid is readonly and contains only text fields. Those text fields contain data that doesn't exceed 50 characters. This is the DataGrid's XAML:

<DataGrid SelectionUnit="Cell" Grid.Column="1" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" ScrollViewer.CanContentScroll="False" DataGrid:SelectedItem.AutoScroll="True" SelectedItem="{Binding Path=SelectedItem}" ItemsSource="{Binding Path=GridData}" Name="DataGrid1" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="{x:Static props:Resources.Header1}" IsReadOnly="True" Binding="{Binding Path=SomeBindingPath}"/>
            <DataGridTextColumn Header="{x:Static props:Resources.Header2}" IsReadOnly="True" Binding="{Binding Path=SomeBindingPath}"/>
            <DataGridTextColumn Header="{x:Static props:Resources.Header3}" IsReadOnly="True" Binding="{Binding Path=SomeBindingPath}"/>
            <DataGridTextColumn Header="{x:Static props:Resources.Header4}" IsReadOnly="True" Binding="{Binding Path=SomeBindingPath}"/>
            <DataGridTextColumn Header="{x:Static props:Resources.Header5}" IsReadOnly="True" Binding="{Binding Path=SomeBindingPath}"/>
            <DataGridTextColumn Header="{x:Static props:Resources.Header6}" IsReadOnly="True" Binding="{Binding Path=SomeBindingPath}" Visibility="{Binding Path=DataContext.ColumnVisibility, RelativeSource={x:Static RelativeSource.Self}}"/>
            <DataGridTextColumn Header="{x:Static props:Resources.Header7}" IsReadOnly="True" Binding="{Binding Path=SomeBindingPath}"/>
            <DataGridTextColumn Header="{x:Static props:Resources.Header8}" IsReadOnly="True" Binding="{Binding Path=SomeBindingPath}"/>
            <DataGridTextColumn Header="{x:Static props:Resources.Header9}" IsReadOnly="True" Binding="{Binding Path=SomeBindingPath}"/>
            <DataGridTextColumn Header="{x:Static props:Resources.Header10}" IsReadOnly="True" Binding="{Binding Path=SomeBindingPath}"/>
            <DataGridTextColumn Header="{x:Static props:Resources.Header11}" IsReadOnly="True" Binding="{Binding Path=SomeBindingPath}"/>
            <DataGridTextColumn Header="{x:Static props:Resources.Header12}" IsReadOnly="True" Binding="{Binding Path=SomeBindingPath}"/>
        </DataGrid.Columns>
      </DataGrid>

It takes 30 seconds and 300 MB of RAM to load the DataGrid. This is way too much. How do I fix it?

I thought that DataGrids have virtualization configured by default, but it doesn't seem so. I've tried adding VirtualizingStackPanel.IsVirtualizing="False" to DataGrid and DataGridTextColumn elements, but this didn't help.

As I've already said, there is only 3000 rows of data, so I don't see any point in implementing data virtualization. Or maybe I should?

I've also tried adding following XAML:

  <DataGrid.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel/>
            </ItemsPanelTemplate>
        </DataGrid.ItemsPanel>

Upvotes: 2

Views: 2187

Answers (3)

paparazzo
paparazzo

Reputation: 45096

If it is read only then go with GridView ListView. And use TextBlock (not TextBox). DataGrid is a heavy weight control and with it comes a lot of overhead. ListView has good UI virtualization (use recycling and deferrred scrolling). You may need to go to Data Virtualization but I would start with optimizing the UI. I have some very big list in ListView with great performance.

Had a comment on GridView and a proposed edit to remove it. The real worker is the ListView. The GridView is just the formatting. You list 12 columns and to get columns to allign GridView is clean and easy. ListView with a template (and no GridView) is an option - just not the option I would use in this case.

Upvotes: 4

Bogdan Verbenets
Bogdan Verbenets

Reputation: 26936

CanContentScroll should be set to true. If I set it to false I am forcing virtualization to be turned off.

Upvotes: 0

CodeWarrior
CodeWarrior

Reputation: 7468

I would suggest looking into Data Virtualization.

There is a good CodeProject article about it here and a good blog post explaining some of the ins and outs here.

Essentially, you only cache chunks of the data (manageable amounts) and the rest is placeholders in the itemscontrol. As you scroll more items into view, items on the end are loaded, and items that you previously have viewed can either be retained or discarded as you like. It all depends on your circumstances.

It really depends on your data how you will implement, so I wont yet attempt an example.

Upvotes: 2

Related Questions