private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Startup = true;
            // Data source is handled in XAML !!!!
            if (this.BankGrid.Items.Count > 0)
            {
                this.BankGrid.Items.Clear( );
            }
            this.BankGrid.ItemsSource = BankViewercollection;

            if (BankViewercollection.Count == 0)
            {
                BankViewercollection = BankCollection.LoadBank(4, false);
            }
            this.BankGrid.ItemsSource = BankViewercollection;
            this.MouseDown           += delegate { DoDragMove( ); };
            DataFields.DataContext    = this.BankGrid.SelectedItem;

            EventControl.ViewerDataHasBeenChanged += ExternalDataUpdate;                    // Callback in THIS FILE
            //Subscribe to Bank Data Changed event declared in EventControl
            EventControl.BankDataLoaded += EventControl_BankDataLoaded;
            SaveBttn.IsEnabled           = false;
            Startup    = false;
            Count.Text = this.BankGrid.Items.Count.ToString( );
        }
        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Flags.MultiViewer = this;

            EventControl.BankDataLoaded += UpdatedDataLoaded;

            EventControl.CustDataLoaded += UpdatedDataLoaded;

            EventControl.DetDataLoaded += UpdatedDataLoaded;

            EventControl.ViewerDataHasBeenChanged += EventControl_ViewerDataHasBeenChanged;

            if (MultiBankcollection == null || MultiBankcollection.Count == 0)
            {
                MultiBankcollection = BankCollection.LoadBank(3);
            }
            BankGrid.ItemsSource = MultiBankcollection;
            if (MultiCustcollection == null || MultiCustcollection.Count == 0)
            {
                MultiCustcollection = CustCollection.LoadCust(MultiCustcollection);
            }
            if (MultiDetcollection == null || MultiDetcollection.Count == 0)
            {
                MultiDetcollection = DetCollection.LoadDet(MultiDetcollection);
            }
            this.BankGrid.ItemsSource     = MultiBankcollection;
            this.CustomerGrid.ItemsSource = MultiCustcollection;
            this.DetailsGrid.ItemsSource  = MultiDetcollection;
        }
        private async void ReLoadAllDataBases( )
        {
            this.BankGrid.ItemsSource     = null;
            this.CustomerGrid.ItemsSource = null;
            this.DetailsGrid.ItemsSource  = null;

            this.BankGrid.Items.Clear( );
            this.CustomerGrid.Items.Clear( );
            this.DetailsGrid.Items.Clear( );

            BankCollection.LoadBank(3);
            MultiBankcollection = BankCollection.LoadBank(3);
            MultiCustcollection = CustCollection.LoadCust(MultiCustcollection);
            MultiDetcollection  = DetCollection.LoadDet(MultiDetcollection);
            int b = bindex;
            int c = cindex;
            int d = dindex;

            this.BankGrid.ItemsSource     = MultiBankcollection;
            this.CustomerGrid.ItemsSource = MultiCustcollection;
            this.DetailsGrid.ItemsSource  = MultiDetcollection;
            bindex = b;
            cindex = c;
            dindex = d;
            this.BankGrid.Refresh( );
            this.CustomerGrid.Refresh( );
            this.DetailsGrid.Refresh( );
        }
        void ReloadData(DataGrid DGrid)
        {
            try
            {
                Mouse.OverrideCursor = Cursors.Wait;
                // Make sure we are back on UI thread

                int current = 0;
                current = DGrid.SelectedIndex == -1 ? 0 : DGrid.SelectedIndex;
                this.BankGrid.ItemsSource     = null;
                this.CustomerGrid.ItemsSource = null;
                this.DetailsGrid.ItemsSource  = null;

                BankCollection.LoadBank(3);
                this.BankGrid.ItemsSource = MultiBankcollection;

                MultiCustcollection           = CustCollection.LoadCust(MultiCustcollection);
                this.CustomerGrid.ItemsSource = MultiCustcollection;

                MultiDetcollection           = DetCollection.LoadDet(MultiDetcollection);
                this.DetailsGrid.ItemsSource = MultiDetcollection;

                DGrid.SelectedIndex = current;
                Console.WriteLine($"End of ReloadGrid() : Thread = { Thread . CurrentThread . ManagedThreadId}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"ERROR: ReloadGrid() {ex . Message}, : {ex . Data}");
            }
        }
 /// <summary>
 /// A specialist version  to reload data WITHOUT changing global version
 /// </summary>
 /// <returns></returns>
 public static BankCollection LoadBankTest(BankCollection temp)
 {
     try
     {
         for (int i = 0; i < dtBank.Rows.Count; i++)
         {
             temp.Add(new BankAccountViewModel
             {
                 Id      = Convert.ToInt32(dtBank.Rows [i] [0]),
                 BankNo  = dtBank.Rows [i] [1].ToString( ),
                 CustNo  = dtBank.Rows [i] [2].ToString( ),
                 AcType  = Convert.ToInt32(dtBank.Rows [i] [3]),
                 Balance = Convert.ToDecimal(dtBank.Rows [i] [4]),
                 IntRate = Convert.ToDecimal(dtBank.Rows [i] [5]),
                 ODate   = Convert.ToDateTime(dtBank.Rows [i] [6]),
                 CDate   = Convert.ToDateTime(dtBank.Rows [i] [7]),
             });
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine($"BANK : SQL Error in BankCollection load function : {ex . Message}, {ex . Data}");
         MessageBox.Show($"BANK : SQL Error in BankCollection load function : {ex . Message}, {ex . Data}");
     }
     finally
     {
         Console.WriteLine($"BANK : Completed load into Bankcollection :  {temp . Count} records loaded successfully ....");
     }
     return(temp);
 }
        private async void MultiAccts_Click(object sender, RoutedEventArgs e)
        {
            // Filter data to show ONLY Custoimers with multiple bank accounts

            if (MultiAccounts.Content != "Show All")
            {
                Flags.IsMultiMode = true;
                BankCollection bank = new BankCollection();
                bank = await bank.ReLoadBankData( );

                this.BankGrid.ItemsSource = null;
                this.BankGrid.ItemsSource = bank;
                this.BankGrid.Refresh( );
                ControlTemplate tmp = Utils.GetDictionaryControlTemplate("HorizontalGradientTemplateGray");
                MultiAccounts.Template = tmp;
                Brush br = Utils.GetDictionaryBrush("HeaderBorderBrushRed");
                MultiAccounts.Background = br;
                MultiAccounts.Content    = "Show All";
                Count.Text = this.BankGrid.Items.Count.ToString();
            }
            else
            {
                Flags.IsMultiMode = false;
                BankCollection bank = new BankCollection();
//				bank = await bank . ReLoadBankData ( );
                // Just reset our iremssource to man Db
                this.BankGrid.ItemsSource = null;
                this.BankGrid.ItemsSource = BankViewercollection;
                this.BankGrid.Refresh( );
                ControlTemplate tmp = Utils.GetDictionaryControlTemplate("HorizontalGradientTemplateBlue");
                MultiAccounts.Template = tmp;
                Brush br = Utils.GetDictionaryBrush("HeaderBrushBlue");
                MultiAccounts.Background = br;
                MultiAccounts.Content    = "Multi Accounts";
                Count.Text = this.BankGrid.Items.Count.ToString( );
            }


            //			BankAccountViewModel bank = new BankAccountViewModel();
            //			var filtered = from bank inBankViewercollection . Where ( x => bank . CustNo = "1055033" ) select x;
            //		   GroupBy bank.CustNo having count(*) > 1
            //where
            //having COUNT (*) > 1
            //	select bank;
            //	Where ( b.CustNo = "1055033") ;

            /*
             *                      commandline = $"SELECT * FROM BANKACCOUNT WHERE CUSTNO IN "
             + $"(SELECT CUSTNO FROM BANKACCOUNT "
             + $" GROUP BY CUSTNO"
             + $" HAVING COUNT(*) > 1) ORDER BY ";
             +
             */
        }
        public async static Task <bool> LoadBankTaskInSortOrderasync(bool b = false, int i = -1)
        {
            try
            {
                if (dtBank.Rows.Count > 0)
                {
                    dtBank.Clear( );
                }

                if (Bankcollection.Items.Count > 0)
                {
                    Bankcollection.ClearItems( );
                }

                Bankcollection = null;
                Bankcollection = new BankCollection( );

                Console.WriteLine($"Calling Task.Run in Bankcollection ....");
                await Task.Run(async( ) =>
                {
                    Console.WriteLine($"Calling LoadBankData in Task.Run in Bankcollection ....");
                    await LoadBankData( );
                    Console.WriteLine($"Returned from LoadBankData in Task.Run in Bankcollection ....");

                    Application.Current.Dispatcher.Invoke(
                        async( ) =>
                    {
                        Console.WriteLine($"Calling LoadBankCollection in Task.Run in Bankcollection ....");
                        await LoadBankCollection( );
                        Console.WriteLine($"Returned from LoadBankCollection in Task.Run in Bankcollection ....");
                    });
                });

                Console.WriteLine($"**** END **** OF ASYNC CALL METHOD {dtBank . Rows . Count} records in DataTable, {Bankcollection . Count} in Bankcollection ....");
                Console.WriteLine($"**** END **** SENDING CALLBACK MESSAGE TO SQLDBVIEWER WINDOW TO LOAD THEIR DATAGRID !!!");
                if (BankDataLoaded != null)
                {
                    BankDataLoaded.Invoke(Bankcollection, new LoadedEventArgs {
                        CallerDb = "BANKACCOUNT", DataSource = Bankcollection
                    });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"ERROR in LoadBankTaskInSortOrderAsync() : {ex . Message}, : {ex . Data}");
                return(false);
            }
            return(true);
        }
        public static bool SelectViewer(int ViewerType, BankCollection tmp)
        {
            bool result = false;

            switch (ViewerType)
            {
            case 1:
                SqlViewerBankcollection = tmp;
                result = true;
                break;

            case 2:
                EditDbBankcollection = tmp;
                result = true;
                break;

            case 3:
                MultiBankcollection = tmp;
                result = true;
                break;

            case 4:
                BankViewerDbcollection = tmp;
                result = true;
                break;

            //case 5:
            //	CustViewerDbcollection = tmp;
            //	result = true;
            //	break;
            //case 6:
            //	DetViewerDbcollection = tmp;
            //	result = true;
            //	break;
            //case 7:
            //	SqlViewerCustcollection = tmp;
            //	result = true;
            //	break;
            //case 8:
            //	SqlViewerDetcollection = tmp;
            //	result = true;
            //	break;
            case 9:
//					= tmp;
                result = true;
                break;
            }
            return(result);
        }
        public async static Task <bool> LoadBankCollection(bool Notify = false)
        {
            int count = 0;

            try
            {
                BankCollection bc = new BankCollection( );
                for (int i = 0; i < dtBank.Rows.Count; i++)
                {
                    Bankinternalcollection.Add(new BankAccountViewModel
                    {
                        Id      = Convert.ToInt32(dtBank.Rows [i] [0]),
                        BankNo  = dtBank.Rows [i] [1].ToString( ),
                        CustNo  = dtBank.Rows [i] [2].ToString( ),
                        AcType  = Convert.ToInt32(dtBank.Rows [i] [3]),
                        Balance = Convert.ToDecimal(dtBank.Rows [i] [4]),
                        IntRate = Convert.ToDecimal(dtBank.Rows [i] [5]),
                        ODate   = Convert.ToDateTime(dtBank.Rows [i] [6]),
                        CDate   = Convert.ToDateTime(dtBank.Rows [i] [7]),
                    });
                    count = i;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"BANK : SQL Error in BankCollection load function : {ex . Message}, {ex . Data}");
                MessageBox.Show($"BANK : SQL Error in BankCollection load function : {ex . Message}, {ex . Data}");
            }
            finally
            {
                BankCollection bc = new BankCollection( );
                if (Notify)
                {
                    EventControl.TriggerBankDataLoaded(null,
                                                       new LoadedEventArgs
                    {
                        CallerDb   = "BankAccount",
                        DataSource = bc,
                        RowCount   = Bankinternalcollection.Count
                    });
                }
            }
            //			Flags . BankCollection = Bankcollection;
            return(true);
        }
        /// <summary>
        /// Main Method for loading Bank Account data to our global BankCollection.Basnkcollection
        /// </summary>
        /// <returns></returns>
        public static BankCollection LoadBank(int ViewerType, bool NotifyAll = false)
        {
            // Called to Load/reload the One & Only Bankcollection data source
            if (dtBank.Rows.Count > 0)
            {
                dtBank.Clear( );
            }

            if (Bankinternalcollection == null)
            {
                Bankinternalcollection = new BankCollection( );
                Console.WriteLine($"\n ***** SQL WARNING Created a NEW MasterBankCollection ...................");
            }

            // Load data fro SQL into dtBank Datatable
            LoadBankData( );

            if (Bankinternalcollection == null)
            {
                Bankinternalcollection = new BankCollection( );
                Console.WriteLine($"\n ***** SQL WARNING Created a NEW Bankinternalcollection ...................");
            }
            if (Bankinternalcollection.Count > 0)
            {
                Bankinternalcollection.ClearItems( );
            }

            // this returns "Bankinternalcollection" as a pointer to the correct viewer
            LoadBankCollection( );

            // We now have the pointer to the the Bank data in variable Bankinternalcollection
            if (Flags.IsMultiMode == false)
            {
                // Finally fill and return The global Dataset
                SelectViewer(ViewerType, Bankinternalcollection);
                return(Bankinternalcollection);
            }
            else
            {
                // return the "working  copy" pointer, it has  filled the relevant collection to match the viewer
                return(Bankinternalcollection);
            }
        }
        /// <summary>
        /// Method used ONLY when working with Multi accounts data
        /// </summary>
        /// <param name="b"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public async Task <BankCollection> ReLoadBankData(bool b = false, int mode = -1)
        {
            if (dtBank.Rows.Count > 0)
            {
                dtBank.Clear( );
            }

            //await LoadBankTaskInSortOrderasync ( false );
            BankCollection temp = new BankCollection();

            if (temp.Count > 0)
            {
                temp.ClearItems( );
            }
            LoadBankData( );
            if (Flags.IsMultiMode)
            {
                // Loading  subset of multi accounts only
                //				BankCollection bank = new BankCollection();
                temp = LoadBankTest(temp);
                // Just return  the subset of data without updating our
                // //Flags pointer or class Bankcollection pointer
                return(temp);
            }
            else
            {
                // :Loading full total or data
                Bankinternalcollection = LoadBank(mode);
                SelectViewer(mode, Bankinternalcollection);

                // Set our globals etc
                //				Bankcollection = Bankinternalcollection;
                //				Flags . BankCollection = Bankcollection = Bankinternalcollection;
                return(Bankinternalcollection);
            }
        }
 public BankCollection( ) : base( )
 {
     //set the static pointer to this class
     Bankcollection = this;
 }