Results 1 to 2 of 2

Thread: Need help understanding MVVM model WPF DataGrid

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    48

    Need help understanding MVVM model WPF DataGrid

    I have made small C# application for displaying Database in WPF DataGrid. Everything works fine but as I have two windows in application, I need to load Database separately to both of them. I would like to load it once and be able to filter data separately in a different way in two different windows. I have understood that the right way to proceed is to create MVVM model?

    I have managed to build what I have at the moment, however it is not functioning correct. I don't understand how data load should be triggered correct in MVVM model to get values on DataGrid? My current solution does not show any errors but it also is not loading any data to DataGrid.

    C#:

    Code:
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Data.Odbc;
    using System.Windows;
    using System.Windows.Input;
    
    namespace DB_inspector
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                DataContext = new ViewModel();
            }
        }
    
        public class ViewModel : INotifyPropertyChanged
        {
            public ICommand myCommand => new RelayCommand(obj =>
            {
                try
                {
                    string connectionStringDE = "Driver={Pervasive ODBC Client Interface};ServerName=DB123;dbq=@DBFSSE;Uid=ADMIN;Pwd=123;";
    
                    string queryStringDE = "select NRO,NAME,NAMEA,NAMEB,ADDRESS,POSTA,POSTN,POSTB,CORPORATION,COUNTRY,ID,ACTIVE from COMPANY";
    
                    string connectionStringFR = "Driver={Pervasive ODBC Client Interface};ServerName=DB123;dbq=@DBFSFI;Uid=ADMIN;Pwd=123;";
    
                    string queryStringFR = "select NRO,NAME,NAMEA,NAMEB,ADDRESS,POSTA,POSTN,POSTB,CORPORATION,COUNTRY,ID,ACTIVE from COMPANY";
    
                    DataTable dataTable = new DataTable("COMPANY");
                    // using-statement will cleanly close and dispose unmanaged resources i.e. IDisposable instances
                    using (OdbcConnection dbConnectionDE = new OdbcConnection(connectionStringDE))
                    {
                        dbConnectionDE.Open();
                        OdbcDataAdapter dadapterDE = new OdbcDataAdapter();
                        dadapterDE.SelectCommand = new OdbcCommand(queryStringDE, dbConnectionDE);
    
                        dadapterDE.Fill(dataTable);
    
                    }
                    using (OdbcConnection dbConnectionFR = new OdbcConnection(connectionStringFR))
                    {
                        dbConnectionFR.Open();
                        OdbcDataAdapter dadapterFR = new OdbcDataAdapter();
                        dadapterFR.SelectCommand = new OdbcCommand(queryStringFR, dbConnectionFR);
    
                        var newTable = new DataTable("COMPANY");
                        dadapterFR.Fill(newTable);
    
                        dataTable.Merge(newTable);
    
                    }
    
                    _ = dataTable.DefaultView;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            });
    
            private bool _allowUIChanges = true;
            public bool AllowUIChanges
            {
                get => _allowUIChanges;
                set
                {
                    _allowUIChanges = value;
                    OnPropertyChanged(nameof(AllowUIChanges));
                    OnPropertyChanged(nameof(IsReadOnlyDataGrid));
                }
            }
    
            private void OnPropertyChanged(string v)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(v));
            }
    
            public bool IsReadOnlyDataGrid
            {
                get => !_allowUIChanges;
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
        }
    
        public class RelayCommand : ICommand
        {
            private readonly Action<object> _execute;
            private readonly Func<object, bool> _canExecute;
    
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }
            public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
            {
                _execute = execute;
                _canExecute = canExecute;
            }
            public bool CanExecute(object parameter) => _canExecute == null || _canExecute(parameter);
    
            public void Execute(object parameter) => _execute(parameter);
        }
    }
    XAML:

    Code:
    <Window x:Class="DB_inspector.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            mc:Ignorable="d"
            Title="DB database inspector" Height="595.404" Width="1005.571">
        <Grid Background="White">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Button Grid.Row="0" Content="Load" Command="{Binding Load}" HorizontalAlignment="Center"/>
            <DataGrid Grid.Row="1" ItemsSource="{Binding Items}"/>
        </Grid>
    </Window>

  2. #2

    Re: Need help understanding MVVM model WPF DataGrid

    Hello Mrwad,

    Check this link for the basic understanding of how the MVVM model can work

    https://www.c-sharpcorner.com/Upload...20difficulties.

    https://www.wintellect.com/model-vie...vvm-explained/

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