Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Problem to synchronise two DataContext bound ComboBoxes

Problem to synchronise two DataContext bound ComboBoxes

Scheduled Pinned Locked Moved WPF
csharpdatabasewpflinqhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tom Rahav
    wrote on last edited by
    #1

    Hi, I’m trying to do something quite simple, I believe, but am very new to WPF and can’t figure it out. I want to display bank account details of a specific employee (which I select separately), and I want to let the user change those details if needed. My problem concerns specifically the following 3 tables: Banks: - BankID (PK) - CountryID - BankName - ... Countries: - CountryID (PK) - CountryName - ... BankAccounts: - BankAccountID (PK) - BankID - ... On my application’s MainWindow I have 2 ComboBoxes. I want to load the first one (cmbBankCountry) with a list of all the countries from ‘Countries’ table, and then automatically select the country which corresponds to the employee’s bank account. Similarly, I want the second ComboBox (cmbBanks) to be loaded with the names of all the banks that are associated with the selected country, and here too, select automatically the bank associated with that employee’s bank account. Of course, if the user changes the selection of the country for that bank account, the second ComboBox should be reloaded and display only the banks associated with the newly selected country. All the data comes from an SQL database via Linq-To-SQL classes (dbml), which I generated to represent the database tables. I.e. I have a Bank class, a Country class, a BankAccount class, etc. defined in a DataContext that I declared as a Private Shared variable of Application. I also created a ReadOnly property called DBMain to access it. So far I tried to tackle this task as follows: To load the first Combobox with all the countries I placed the following code in the MainWindow_Loaded event:

    Dim lstCountries As List(Of Country) = Application.DBMain.GetCountries()
    cmbBankCountry.ItemsSource = lstCountries
    cmbBankCountry.DisplayMemberPath = "CountryName"
    cmbBankCountry.SelectedValuePath = "CountryID"

    The GetCountries function looks like this:

    Public Function GetCountries() As List(Of Country)

            Dim lstCountry As List(Of Country) = (From c In Application.DBMain.Countries
                                                                      Select c
                                                                    ).ToList
            Return lstCountry
    

    End Function

    To load the second Combobox with the corresponding banks, I created an event that is triggered when the first Combobox’s selection (country) is changed:

    Private Sub DisplayBanksForCountry()

        Dim lstBanks As
    
    L 1 Reply Last reply
    0
    • T Tom Rahav

      Hi, I’m trying to do something quite simple, I believe, but am very new to WPF and can’t figure it out. I want to display bank account details of a specific employee (which I select separately), and I want to let the user change those details if needed. My problem concerns specifically the following 3 tables: Banks: - BankID (PK) - CountryID - BankName - ... Countries: - CountryID (PK) - CountryName - ... BankAccounts: - BankAccountID (PK) - BankID - ... On my application’s MainWindow I have 2 ComboBoxes. I want to load the first one (cmbBankCountry) with a list of all the countries from ‘Countries’ table, and then automatically select the country which corresponds to the employee’s bank account. Similarly, I want the second ComboBox (cmbBanks) to be loaded with the names of all the banks that are associated with the selected country, and here too, select automatically the bank associated with that employee’s bank account. Of course, if the user changes the selection of the country for that bank account, the second ComboBox should be reloaded and display only the banks associated with the newly selected country. All the data comes from an SQL database via Linq-To-SQL classes (dbml), which I generated to represent the database tables. I.e. I have a Bank class, a Country class, a BankAccount class, etc. defined in a DataContext that I declared as a Private Shared variable of Application. I also created a ReadOnly property called DBMain to access it. So far I tried to tackle this task as follows: To load the first Combobox with all the countries I placed the following code in the MainWindow_Loaded event:

      Dim lstCountries As List(Of Country) = Application.DBMain.GetCountries()
      cmbBankCountry.ItemsSource = lstCountries
      cmbBankCountry.DisplayMemberPath = "CountryName"
      cmbBankCountry.SelectedValuePath = "CountryID"

      The GetCountries function looks like this:

      Public Function GetCountries() As List(Of Country)

              Dim lstCountry As List(Of Country) = (From c In Application.DBMain.Countries
                                                                        Select c
                                                                      ).ToList
              Return lstCountry
      

      End Function

      To load the second Combobox with the corresponding banks, I created an event that is triggered when the first Combobox’s selection (country) is changed:

      Private Sub DisplayBanksForCountry()

          Dim lstBanks As
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Check out this link on "WPF parent-child combo boxes": http://stackoverflow.com/questions/2882006/wpf-binding-comboboxes-to-parent-child-model[^]

      T 1 Reply Last reply
      0
      • L Lost User

        Check out this link on "WPF parent-child combo boxes": http://stackoverflow.com/questions/2882006/wpf-binding-comboboxes-to-parent-child-model[^]

        T Offline
        T Offline
        Tom Rahav
        wrote on last edited by
        #3

        Fantastic! Thanks!!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups