Home > Software engineering >  Why is the ComboBoxColumn very slow on drop-down open in DataGrid WPF?
Why is the ComboBoxColumn very slow on drop-down open in DataGrid WPF?

Time:09-04

I am using C# WPF and currently, I am loading some data from the database in Datagrid.

I loading more than 24,000 rows from a table in the database into DataGridComboBoxColumn, the problem is that when I open the Combobox it is very slow so it takes about 30 seconds to display the records

I solved that problem in DataGridTemplateColumn here is the XAML :

    <DataGridTemplateColumn Header="ComboBox Element" Width="120">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox x:Name="ComboBox_Commodity"
                                          ItemsSource="{Binding Path=TheCommodityCombo_DATA, RelativeSource={RelativeSource AncestorType=Window}}"
                                          SelectedValue="{Binding CommodityID}"
                                          DisplayMemberPath="CommodityName"
                                          SelectedValuePath="CommodityCode"
                                          
                                          IsTextSearchEnabled="True"
                                          IsEditable="True"
                                          SelectedIndex="0"  BorderBrush="#FFADEEB4" Background="{x:Null}" BorderThickness="1" PreviewLostKeyboardFocus="ComboBox_Commodity_PreviewLostKeyboardFocus">
                                    <ComboBox.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <VirtualizingStackPanel VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling"/>
                                        </ItemsPanelTemplate>
                                    </ComboBox.ItemsPanel>
                                </ComboBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

but I don't want use DataGridTemplateColumn because in the combobox element doen't firing the CellEndEdit event

so I'm using DataGridComboBoxColumn

XAML:

    <DataGridComboBoxColumn Width="160" Header="DataGridComboBoxColumn"
                                            SelectedValueBinding="{Binding CommodityID}" 
                                            DisplayMemberPath="CommodityName" 
                                            SelectedValuePath="CommodityCode">
                        <DataGridComboBoxColumn.ElementStyle>
                            <Style TargetType="{x:Type ComboBox}">
                                <Setter Property="ItemsSource" Value="{Binding Path=TheCommodityCombo_DATA, RelativeSource={RelativeSource AncestorType=Window}}" />
                            </Style>
                        </DataGridComboBoxColumn.ElementStyle>
                        
                        <DataGridComboBoxColumn.EditingElementStyle>
                            <Style TargetType="{x:Type ComboBox}">
                                <Setter Property="ItemsSource" Value="{Binding Path=TheCommodityCombo_DATA, RelativeSource={RelativeSource AncestorType=Window}}" />
                                <Setter Property="SelectedIndex" Value="0"/>
                                <Setter Property="IsEditable" Value="True"/>
                                
                                <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
                                <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
                                <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>
                            </Style>
                        </DataGridComboBoxColumn.EditingElementStyle>
                    </DataGridComboBoxColumn>

My issue is : the settings that I made exactly like the ComboBox in DataGridTemplateColumn do not work for the DataGridComboBoxColumn! and DataGridComboBoxColumn on opening combobox is so much slow

How do I fix this ↑ ?

similar problem but in Templatecolumn : Combox column in wpf datagrid performance issue

CodePudding user response:

I am unable to test the solution below and it does not fit in a comment but it seems like you are trying to style VirtualizingStackPanel without first creating one for a ComboBox.

First define your ItemsPanelTemplate:

<Window.Resources>
  <ItemsPanelTemplate x:Key="VSP">
     <VirtualizingStackPanel IsVirtualizing="True" VirtualizationMode="Recycling"/>
  </ItemsPanelTemplate>
</Window.Resources>

Then style your DataGridComboBoxColumn's ComboBox to use this ItemsPanelTemplate.

<Style TargetType="{x:Type ComboBox}">
  <Setter Property="ItemsPanel" Value="{StaticResource VSP}" />

This should replace ItemsPanel with your configured VirtualizingItemsPanel.

Source: http://vbcity.com/blogs/xtab/archive/2009/12/15/wpf-using-a-virtualizingstackpanel-to-improve-combobox-performance.aspx

CodePudding user response:

ComboBox element can fire the CellEndEdit event

ComboBox should be contained in CellEditingTemplate instead of CellTemplate

<DataGrid ItemsSource="{Binding ItemList}" x:Name="dataGrid" CellEditEnding="dataGrid_CellEditEnding">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="ComboBox Element" Width="120">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding ItemList}"
                                                IsEditable="True">
                    </ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

ComboBox is created(Initialized) when entering edit mode..

It will help improve performance

  • Related