-
Nov 16th, 2019, 07:47 AM
#1
Thread Starter
Addicted Member
Loading data to Datagrid via Model-View-ViewModel (MVVM)
Code:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Button x:Name="Button1" Grid.Row="0" Width="100" Content="Fill DataGrid"/>
<DataGrid x:Name="DataGrid1" Grid.Row="1"/>
<Label x:Name="Label1" Grid.Row="2" Background="Yellow"/>
</Grid>
</Window>
My question is here:
Loading 7 columns and 1048576 rows to the DataGrid1 takes some time which I dont want.
If I can eliminate time consumption by implementing Model-View-ViewModel (MVVM) then please tell me how...
Edit: As you can easily understand creating items and adding them to collection takes time, not loading to DataGrid.
Last edited by Kram Kramer; Nov 16th, 2019 at 02:21 PM.
-
Nov 16th, 2019, 08:34 AM
#2
Re: Loading data to Datagrid via Model-View-ViewModel (MVVM)
You're surprised that loading over a million rows takes time? MVVM is a design pattern to make code more maintainable, not a magic wand to make UI's defy the laws of nature. You should absolutely be using MVVM to build WPF apps so you should learn how fairly quickly. It's OK to learn the basics of WPF without MVVM but don't get too used to writing WPF code like you did for Windows Forms, or you'll end up having to unlearn certain things. As for this issue, do as any sensible developer would with that much data in any UI framework: virtualise.
https://www.codeproject.com/Articles...Virtualization
-
Nov 16th, 2019, 09:10 AM
#3
Thread Starter
Addicted Member
Re: Loading data to Datagrid via Model-View-ViewModel (MVVM)
jmcilhinney
As you can easily understand creating items and adding them to collection takes time, not loading to DataGrid.
So the article you linked which is regarding Data Virtualization is not related to my question.
-
Nov 16th, 2019, 09:48 AM
#4
Re: Loading data to Datagrid via Model-View-ViewModel (MVVM)
Originally Posted by Kram Kramer
jmcilhinney
As you can easily understand creating items and adding them to collection takes time, not loading to DataGrid.
So the article you linked which is regarding Data Virtualization is not related to my question.
Except it is related because the whole point is that you don't create the entire list in one go at the start but rather get the data in pages. If you're saying yourself that it is creating the list in the first place that is the issue then what does this even have to do with WPF or MVVM?
Last edited by jmcilhinney; Dec 17th, 2020 at 11:59 PM.
-
Dec 17th, 2020, 11:22 PM
#5
Member
Re: Loading data to Datagrid via Model-View-ViewModel (MVVM)
Hello,
Please follow these steps, To Loading data to Datagrid via Model-View-ViewModel (MVVM)
Step 1: you created a window, its view model, and connected them via DataContext.
Code:
View.MainWindow mainWin = new View.MainWindow();
ViewModel.BookViewModel bookViewModel = new ViewModel.BookViewModel();
mainWin.DataContext = bookViewModel;
Step 2:Binding in xaml.
Code:
<DataGrid Name="BooksDataGrid" ItemsSource="{Binding Books}">
<DataGrid.Columns>
<DataGridTextColumn Header="Title" Width="200" Binding="{Binding title}"/>
<DataGridTextColumn Header="isbn" Width="200" Binding="{Binding isbn}"/>
</DataGrid.Columns>
</DataGrid>
Step 3:Binding works with properties.
Code:
class BookViewModel
{
public ObservableCollection<Book> Books { get; private set; }
public BookViewModel()
{
Books = new ObservableCollection<Book>();
}
}
Step 4:implement load data method in the view model and call it from command.
I hope this information will be useful to you,
Thank you.
< advertising removed by moderator >
-
Dec 18th, 2020, 09:59 AM
#6
Re: Loading data to Datagrid via Model-View-ViewModel (MVVM)
Good job botting a subject line only, ignoring the actual question, and pasting content word for word from here:
https://stackoverflow.com/questions/...nd-mvvm-in-wpf
-
Dec 18th, 2020, 11:26 AM
#7
Re: Loading data to Datagrid via Model-View-ViewModel (MVVM)
I have no issue with posting code from elsewhere as long as it's not claimed as your own.
So next time please do post the relative thread as there might be more answers there that the OP can take advantage of.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|