Why is the ComboBoxColumn very slow on drop-down open in DataGrid WPF?

Mojtaba_Hakim 281 Reputation points
2022-09-03T07:33:01.693+00:00

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

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 ↑ ?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,667 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,204 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
{count} votes