I implemented a datagrid using mvvm and here is how my viewmodel and view looks like as below. my problem is that when i load the products, they will be loaded fast but since each product has a category and manufacturer, it will bind all those in each combobox items. so it means that if i have 100 products, 1000 categories and 1000 manufacturers. it will load and bind those 100 times for each 1000 items. this is causing a big delay in loadtime and also datagrid looks heavy as i try to scroll up-down or left-right, there will be a significant slowness. How can i solve this problem? thanks for your advises.
Public Class ProductsVM Private _SearchKeyword As String Public Property SearchKeyword() As String Get Return _SearchKeyword End Get Set(value As String) If _SearchKeyword <> value Then _SearchKeyword = value RaisePropertyChanged() End If End Set End Property Private m_Products As ObservableCollection(Of Product) Public Property Products() As ObservableCollection(Of Product) Get Return m_Products End Get Set(value As ObservableCollection(Of Product)) m_Products = value End Set End Property Private m_Categorys As ObservableCollection(Of Category) Public Property Categories() As ObservableCollection(Of Category) Get Return m_Categorys End Get Set(value As ObservableCollection(Of Category)) m_Categorys = value End Set End Property Private m_Manufacturers As ObservableCollection(Of Manufacturer) Public Property Manufacturers() As ObservableCollection(Of Manufacturer) Get Return m_Manufacturers End Get Set(value As ObservableCollection(Of Manufacturer)) m_Manufacturers = value End Set End Property Public Sub New() End Sub Private ButtonClick As relaycommand Public Property NewProperty() As relaycommand Get Return ButtonClick End Get Set(ByVal value As relaycommand) ButtonClick = value End Set End Property Protected Overrides Async Sub GetData() Try Dim _dboProducts As New ObservableCollection(Of Product)() Dim dboProducts__1 As New List(Of Product) dboProducts__1 = Await (From c In dboProducts Where c.Name.ToLower.Contains(SearchKeyword.Trim) Order By c.ProductId).ToListAsync() Products = dboProducts__1 RaisePropertyChanged("Products") Dim _dboManufacturers As New ObservableCollection(Of Manufacturer)() Dim dboManufacturers__1 = Await (From c In mycontext.Manufacturer Order By c.ManufacturerId).ToListAsync() Manufacturers = dboManufacturers__1 RaisePropertyChanged("Manufacturers") Dim _dboCategorys As New ObservableCollection(Of Category)() Dim dboCategorys__1 = Await (From c In mycontext.Category Order By c.CategoryId).ToListAsync() Categories = dboCategorys__1 RaisePropertyChanged("Categories") Catch ex As Exception End Try End Sub End Class
<DataGrid.RowStyle><Style TargetType="{x:Type DataGridRow}"><Setter Property="cst:DataGridRowBehavior.IsDataGridRowFocussedWhenSelected" Value="true"/></Style></DataGrid.RowStyle><DataGrid.Columns><DataGridTextColumn Header="ProductId" Binding="{Binding ProductId,Mode=OneWay}" IsReadOnly="True" Width="100" ></DataGridTextColumn><DataGridComboBoxColumn Header="Manufacturer" Width="400" filter:DataGridComboBoxExtensions.UserCanEnterText="True" DisplayMemberPath="Name" SelectedValueBinding="{Binding ManufacturerId}" SelectedValuePath="ManufacturerId"><DataGridComboBoxColumn.ElementStyle><Style TargetType="ComboBox"><Setter Property="ItemsSource" Value="{Binding Path=DataContext.Manufacturers, RelativeSource={RelativeSource AncestorType=DataGrid}}" /></Style></DataGridComboBoxColumn.ElementStyle><DataGridComboBoxColumn.EditingElementStyle><Style TargetType="ComboBox"><Setter Property="ItemsSource" Value="{Binding Path=DataContext.Manufacturers, RelativeSource={RelativeSource AncestorType=DataGrid}}" /></Style></DataGridComboBoxColumn.EditingElementStyle></DataGridComboBoxColumn><DataGridComboBoxColumn Header="Category" Width="400" filter:DataGridComboBoxExtensions.UserCanEnterText="True" DisplayMemberPath="Name" SelectedValueBinding="{Binding CategoryId}" SelectedValuePath="CategoryId"><DataGridComboBoxColumn.ElementStyle><Style TargetType="ComboBox"><Setter Property="ItemsSource" Value="{Binding Path=DataContext.Categories, RelativeSource={RelativeSource AncestorType=DataGrid}}" /></Style></DataGridComboBoxColumn.ElementStyle><DataGridComboBoxColumn.EditingElementStyle><Style TargetType="ComboBox"><Setter Property="ItemsSource" Value="{Binding Path=DataContext.Categories, RelativeSource={RelativeSource AncestorType=DataGrid}}" /></Style></DataGridComboBoxColumn.EditingElementStyle></DataGridComboBoxColumn></DataGrid.Columns></cst:CustomDataGrid></Grid>
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."