View not updating and other issues after CRUD operations in WPF app

don bradman 621 Reputation points
2022-07-10T16:19:29.497+00:00

I have a WPF window with a tabcontrol with 2 tabitems, first one only displays some date filtered data from a SQLite database and the second tabitem also has a tabitem which shows filtered data where data is being filtered by the selecteditem of a combobox and then further filtered by a text box data with a button click.

I'm trying to implement CRUD operations using the selected row of the datagrid in the second tabitem and when the CRUD operations are done then the view in both the datagrid's (in tabitem 1 & 2) along with the combo box items should get updated.

But currently I'm having these issues and errors:

1) When I update a record then the updated record is displayed immediately as it should in the datagrid of the 2nd tabitem but the datagrid of the 1st tabitem does not update the view.
219150-view.gif

2) When I add a new record or delete a record the view (both datagrid's in tabitem1 & 2) does not update and If a new record has a new Party name then that should also be added to the combo box items but it doesn't either.
219263-addnew.gif

219259-del.gif

3) And lastly when adding a new record if I keep the PaidOn column blank then the next time I run the app I get an error:

219260-image.png

How do I fix all these errors?

The onedrive link for my project is here

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,680 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,226 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 38,191 Reputation points Microsoft Vendor
    2022-07-15T09:46:06.643+00:00

    Hi,@don bradman .

    1. The PendingBills and AllBills of the ICollectionView bound to the DataGrid in your two TabItems are m.PBills and m.SBills respectively. The data sources are different, so the views will not change synchronously.

    When the data source of PendingBills and AllBills are the same, the view will change synchronously.

    _penBills = m.PBills;  
    
     PendingBills = new ListCollectionView(_penBills)  
     {  
     Filter = o => ((Bills)o).PaidOn==""  
     };  
    
    _allBills = m.SBills;  
     AllBills = new ListCollectionView(_allBills)  
     {  
     Filter = o => ((Bills)o).Party == SelectedCBItem  
     };  
    

    2.Data CRUD works normally, and reloading program data updates normally. The current problem is that the view is not updated after modification.

    There are three code blocks in the project that load data from the database, all of which are modified as follows. Even if the PaidOn of the newly added Item is empty, no error will be reported.

    public void LoadAllBillsFroBillsbase()  
     {  
     ...  
    
     foreach (DataRow row in dt.Rows)  
     {  
    
     **var p = (row["PaidOn"] == DBNull.Value) ? String.Empty : (string)(row["PaidOn"]);**  
    
     _sBills.Add(new Bills()  
     {  
     Id = Convert.ToInt32(row["Id"]),  
     Party = (string)row["Party"],  
     BillNo = (string)row["BillNo"],  
     BillDt = (string)(row["BillDt"]),  
     Amt = (string)row["Amt"],  
     DueDt = (string)(row["DueDt"]),  
     //PaidOn = (string)(row["PaidOn"])  
     **PaidOn = p**  
     });  
    
     }  
     m_dbConnection.Close();  
     }  
    

    I'm trying to find a solution to update the view in time and will come back if there are any updates.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.