//**************************************************************************************************************************************************************// // Loads data from DataTable into Observable Collection public async Task<bool> LoadDetailsObsCollection () { //if (dtDetails.Rows.Count > 0) // return true; try { //Load the data into our ObservableCollection BankAccounts if (DetailsObs.Count > 0) { DetailsObs.Clear (); } for (int i = 0; i < DetailsViewModel.dtDetails.Rows.Count; ++i) DetailsObs.Add (new DetailsViewModel { Id = Convert.ToInt32 (dtDetails.Rows[i][0]), BankNo = dtDetails.Rows[i][1].ToString (), CustNo = dtDetails.Rows[i][2].ToString (), AcType = Convert.ToInt32 (dtDetails.Rows[i][3]), Balance = Convert.ToDecimal (dtDetails.Rows[i][4]), IntRate = Convert.ToDecimal (dtDetails.Rows[i][5]), ODate = Convert.ToDateTime (dtDetails.Rows[i][6]), CDate = Convert.ToDateTime (dtDetails.Rows[i][7]) }); // WE NOW HAVE OUR DATA HERE - fully loaded into Obs Console.WriteLine ($"Sql data loaded into DetailsObs [{DetailsObs.Count}] ...."); return true; } catch (Exception ex) { Console.WriteLine ($"Error loading Details Data {ex.Message}"); return false; } }
//**************************************************************************************************************************************************************// /// <summary> /// Initialise Data for Details Grid using Task.Factory thread /// and it then triggers the [TriggerCustDataLoad Event] /// and finally ensures the ItemsSource is set to the ObsCollection /// once ithe Event has triggered all theother load code ? /// Called from DBSELECTOR /// </summary> /// <param> </param> public async Task LoadDetailsTask (int mode = -1) { Mouse.OverrideCursor = Cursors.Wait; // load SQL data in DataTable //Create the one and only dtDetails instance if not already there if (dtDetails == null) DetailsViewModel.dtDetails = new DataTable (); else dtDetails.Clear (); try { if (DetailsObs != null && DetailsObs.Count > 0) DetailsObs.Clear (); } catch (Exception ex) { Console.WriteLine ($"DetailsObs Exception [{ex.Data}\r\n"); } DateTime start = DateTime.Now; Console.WriteLine ($"Starting AWAITED task to load Details Data via Sql"); #if USETASK try { // THIS ALL WORKS PERFECTLY - THANKS TO VIDEO BY JEREMY CLARKE OF JEREMYBYTES YOUTUBE CHANNEL int? taskid = Task.CurrentId; Task<DataTable> DataLoader = LoadSqlData (); DataLoader.ContinueWith ( task => { LoadDetailsObsCollection(); }, TaskScheduler.FromCurrentSynchronizationContext () ); Console.WriteLine ($"Completed AWAITED task to load Details Data via Sql\n" + $"task =Id is [{taskid}], Completed status [{DataLoader.IsCompleted}] in {(DateTime.Now - start).Ticks} ticks]\n"); } catch (Exception ex) { Console.WriteLine ($"Task error {ex.Data},\n{ex.Message}"); } Mouse.OverrideCursor = Cursors.Arrow; // WE NOW HAVE OUR DATA HERE - fully loaded into Obs >? #else try { { await LoadSqlData(mode); await LoadDetailsObsCollection (); } } catch (Exception ex) { Console.WriteLine ($"Task error {ex.Data},\n{ex.Message}"); } Mouse.OverrideCursor = Cursors.Arrow; // WE NOW HAVE OUR DATA HERE - fully loaded into Obs >? }