Results 1 to 7 of 7

Thread: Loading data to Datagrid via Model-View-ViewModel (MVVM)

  1. #1

    Thread Starter
    Addicted Member Kram Kramer's Avatar
    Join Date
    Dec 2016
    Posts
    131

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    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

  3. #3

    Thread Starter
    Addicted Member Kram Kramer's Avatar
    Join Date
    Dec 2016
    Posts
    131

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Loading data to Datagrid via Model-View-ViewModel (MVVM)

    Quote Originally Posted by Kram Kramer View Post
    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?

  5. #5
    Member
    Join Date
    Jul 2019
    Location
    Ahmedabad
    Posts
    57

    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 >

  6. #6
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,106

    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

  7. #7
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,594

    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
  •  



Click Here to Expand Forum to Full Width