2

I have a DataGrid with 10-15 columns which can have around 100-200 rows. The datagrid is placed within a Tab control ( which is not in focus by default ).

I tried to virtualize the DataGrid but when I click the tab containing the DataGrid, the program freezes for like 4-5 seconds, and then the tab opens displaying the datagrid. After that the rows seem to scroll fast which is good, but the columns still behave slow like un-virtualized.

When I remove the code to virtualize (last 4 options in DataGrid tag), the grid displays immediately but scrolls very slow and lags.

The following is my datagrid code:

<DataGrid Name="xDataGridFieldConfig" 
     FrozenColumnCount ="1" 
     HorizontalAlignment="Stretch" 
     HorizontalContentAlignment="Stretch" 
     VerticalAlignment="Stretch" 
     Style="{DynamicResource FieldConfigDataGridHeaderStyle}" 
     AutoGenerateColumns="False" 
     CanUserResizeColumns="False" 
     CanUserResizeRows="False" 
     CanUserReorderColumns="False" 
     SelectionMode="Single" 
     GridLinesVisibility="Horizontal" 
     HorizontalGridLinesBrush="#cbcaca" 
     HeadersVisibility="Column" ItemsSource="{Binding FieldConfigCollection}" 
     VerticalScrollBarVisibility="Auto" 
     HorizontalScrollBarVisibility="Visible" 
     VirtualizingPanel.IsVirtualizingWhenGrouping="True" 
     VirtualizingPanel.VirtualizationMode="Standard"  
     VirtualizingPanel.IsVirtualizing="True" 
     ScrollViewer.CanContentScroll="False">

      <DataGrid.Columns>
          <DataGridTextColumn Header="S No." Binding="{Binding Path=ID}" IsReadOnly="True" Width="80"/>

             <!-- using template for custom checkbox -->
             <DataGridTemplateColumn Header="EN" IsReadOnly="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <flatcheckbox:FlatCheckBox x:Name="xFlatCheckBoxFieldConfigEN" Margin="0" IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <flatcheckbox:FlatCheckBox x:Name="xFlatCheckBoxFieldConfigEN" Margin="0" IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>

            </DataGridTemplateColumn>

            <DataGridTemplateColumn Header="Group" Width="150">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                       <TextBlock Name="xTextBlockFieldConfigGroup" Text="{Binding Path=Group, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                       <TextBox Name="xTextBlockFieldConfigGroup" IsEnabled="{Binding Enabled}" Text="{Binding Path=Group, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Padding="0" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

            <!-- 10 similar text-only editable rows -->

       </DataGrid.Columns>

</DataGrid>

Is there anything wrong I'm doing ? How to make the datagrid to be displayed immediately as soon as I open the tab.

mrid
  • 5,782
  • 5
  • 28
  • 71
  • How are you populating the DataGrid?! Does the properties of the object do alot of work? For example I notice that you are using `UpdateSourceTrigger` if it comes from a list of object try to put as `ObservableCollection` and remove the `UpdateSourceTrigger` to see if you notice any diference – Camadas May 14 '19 at 10:42
  • @Camadas I'm using `ObservableCollection` only. But I used UpdateSourceTrigger since the user can edit the checkbox, and textbox – mrid May 14 '19 at 11:12
  • Okidoki, since I never got the "error" that you are getting, at one time did have a DataGrid with more then 300k rows (but only 5 columns) and I didn't have any problem. I do have on a `TabItem` that wasn't showing when the window open, but every time after the content as been rendered I have an async/await to grab and populate the information into the DataGrid. How are you populating the DataGrind? – Camadas May 14 '19 at 11:33
  • performance issue can because only during the rendering: http://thebestcsharpprogrammerintheworld.azurewebsites.net/blogs/WPF-DataGrid-rendering-is-very-slow.aspx – pix May 14 '19 at 15:03
  • If your problem isn't in getting the data then it's almost certainly in measure arrange. You want a fixed size for as much as you can manage. Preferably with all columns fitting in a usual sized window. I notice you have horizontalscrollbarvisibility set to visible. This is not ideal. Fixed width columns with everything in them fixed width, fixed height datagrid, fixed... everything.. will reduce measure-arrange cycles. – Andy May 14 '19 at 15:14
  • The first thing i would look at for a problem are the bindings in your columns. Perhaps there goes more on than you thought. Other things you could check are the `FlatCheckBox` and the style of the `Grid`. – Mardukar May 14 '19 at 15:19

0 Answers0