none
Clear and Refresh bindings of a Combobox in a WPF Grid RRS feed

  • Question

  • This is my first WPF application and I am pulling my hair out, please could someone assist me here.

    I have 2 tables a "Currencies" (ID, Name) and an "ExchangeRates" (CurrencyID, Rate) table.

    Data in the "Currencies" table is as follows:
    1, USD
    2, AUD
    3, EUR etc...
    The ID is the primary key and the Name has a Unique Index.

    Data in the "ExchangeRates" table looks as follows:
    2, 111
    3, 222
    1, 333 etc.
    CurrencyID is the primary key.

    I use LinqToSql classes and on the UI I have a simple WPF DataGrid from the latest WPF toolkit.

    The DataGrid has 2 columns, Currency and Rate, of which the Currency column is bound to a Combobox. The bindings are done in the XAML through normal CollectionViewSources, 1 for the Combobox and 1 for the grid. Using the above data, the grid looks as follows :
    AUD, 111
    EUR, 222
    USD, 333 etc...

    In the code behind the plumbing looks as follows :

    // Bind the Lookup table views
    var currencylist = from fields in DataCont.Currencies
    select new { fields.ID, fields.Name };
    CollectionViewSource currencysource = (CollectionViewSource)FindResource("CurrencyLookup");
    currencysource.Source = currencylist;    <<<< This binds the combobox

    // Bind the main view
    var listdata = from fields in DataCont.ExchangeRates
    select fields;
    MasterViewSource = (
    CollectionViewSource)FindResource("MasterView");
    MasterViewSource.Source = listdata;
    MasterView = (
    BindingListCollectionView)MasterViewSource.View;
    gridData.ItemsSource = MasterView;       <<< This binds the WPF grid


    When the page loads for the first time, everything works 100%, I can add, edit, delete etc. BUT my PROBLEM is that in another page I have the Currency maintenance, so if for example I add a new currency, then I want to go back to this page and refresh the Combobox to show the newly added currency.
    If the user clicks the Refresh button, I am calling exactly the same code as above, BUT, the problem is that the grid now gets populated with the following data :

    USD, 111         <<< PROBLEM, see * below
    EUR, 222            
    USD, 333 etc...

    * This is the problem, as USD is the first record in the Currencies table, so it appears as if the Combobox gets populated, then somehow sets the ExchangeRates to the USD value.
    When I then click on "Save" and call DataCont.SubmitChanges(), I am getting a Primary Key violation error, as there are now 2 records with the same USD currencyId in the ExchangeRates table.

    I have tried to swap the order in which I do the plumbing in the above code behind file, and I have called MasterView.Refresh(), but none of my attempts work and there is also no "Dispose" method to clear the CollectionViews before I re-populate them.

    Any help/advice will be very much appreciated, as I am sure I am doing something wrong. I have followed the exact same approach as per Beth Massi's videos and other samples, but in none of the sameples I could find this "Refresh" functionality.

    Thanks in advance.

     

     

     

     

    Wednesday, June 3, 2009 4:38 PM

Answers

  • Hey, I found the solution !!

    It was as simple as adding the following to the ComboBox, hopefully this can help someone falling in the same trap.

    IsSynchronizedWithCurrentItem

     

    ="False"

    • Marked as answer by Anton Marais Thursday, June 4, 2009 6:19 AM
    Thursday, June 4, 2009 6:19 AM

All replies

  • Does anyone have any information for me to put me on the correct path to refresh specifically the Combobox, as that seems to be the source of the problem ?

    Thanks so much.

    Thursday, June 4, 2009 3:53 AM
  • Hey, I found the solution !!

    It was as simple as adding the following to the ComboBox, hopefully this can help someone falling in the same trap.

    IsSynchronizedWithCurrentItem

     

    ="False"

    • Marked as answer by Anton Marais Thursday, June 4, 2009 6:19 AM
    Thursday, June 4, 2009 6:19 AM