Why do I need to update a bound combobox manually?












2















My Setup



I have following (pseudo) EF classes:



class Department {
int ID,
string Name
}

class DepartmentCustomer {
int ID,
int CustomerID,
Customer Customer,
string Information,
int DepartmentID,
Department Department
}

Class Customer {
int ID,
string Name,
int? CityID,
City City
}

Class City{
int ID,
string Name,
string PostalCode
}


I have a form with a BindingSource for List(of DepartmentCustomer) and the following fields :





  • DepartmentCustomer.Information as a (read-write) textbox


  • DepartmentCustomer.Customer.Name as a (readonly) textbox


  • DepartmentCustomer.Customer.City as a (read-write) combobox.


CustomerForm



The combobox for the city name has following properties:



ComboBoxCity.DataSource = ListOfCities  ' = List(Of City)
ComboBoxCity.ValueMember = "Id"
ComboBoxCity.DisplayMember = "Name"
ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))


My Problem



When DepartmentCustomerBindingSource.CurrentItemChanged occurs, the Combobox is not synchronized; it sometimes updates to the correct value, but when navigating further through the BindingSource it keeps the previous item selected



So, I have to do the following to update the combobox manually



Private Sub DepartmentCustomerBindingSource_CurrentItemChanged(sender As Object, e As EventArgs) Handles DepartmentCustomerBindingSource.CurrentItemChanged
If DepartmentCustomerBindingSource.Current.Contact.City Is Nothing then
ComboBoxCity.SelectedIndex = -1
Else
ComboBoxCity.SelectedItem = DepartmentCustomerBindingSource.Current.Contact.City
End if
End Sub


(for the sake of simplicity I have ommitted casting in my code above. Also these example classes might not make sense IRL)



EDIT



Even the code above does not do what I want.
For example: I have two instances of DepartmentCustomer, one with a Contact.City, the second without a Contact.City . When the form opens at the first instance, it shows the city; when I navigate to the second record, the combobox becomes empty, but when I go back to the first combobox, it stays empty; even more surprising, the first record has been updated to Contact.City = Nothing :'(



EDIT 2: My own not-preferred solution



I have removed the databinding from the combobox (ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))) and added the following sub



Private Sub ComboBoxCity_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBoxCity.SelectedValueChanged
DepartmentCustomerBindingSource.Current.Contact.City = ComboBoxCity.SelectedItem
End Sub


This works, but since I have quite a lot of these comboboxes in my form, I think there has to be an "automatic" way of synchronizing a two-way bound combobox...



Edit 3: I throw in the towel



Even my above "solution" is not working as expected; the Contact.City of the instances are not updated correctly when using the above code...



My Question



Why do I have to to this manually; Am I missing something? I would think that by setting the DataBinding, it would update the SelectedItem whenever the bound BindingSource is being navigated;










share|improve this question

























  • Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBinding​List not (correctly) implemented.

    – Marco Guignard
    Nov 15 '18 at 9:40











  • @MarcoGuignard thanks!!! see my answer below (and my additional question)

    – intrixius
    Nov 15 '18 at 10:01
















2















My Setup



I have following (pseudo) EF classes:



class Department {
int ID,
string Name
}

class DepartmentCustomer {
int ID,
int CustomerID,
Customer Customer,
string Information,
int DepartmentID,
Department Department
}

Class Customer {
int ID,
string Name,
int? CityID,
City City
}

Class City{
int ID,
string Name,
string PostalCode
}


I have a form with a BindingSource for List(of DepartmentCustomer) and the following fields :





  • DepartmentCustomer.Information as a (read-write) textbox


  • DepartmentCustomer.Customer.Name as a (readonly) textbox


  • DepartmentCustomer.Customer.City as a (read-write) combobox.


CustomerForm



The combobox for the city name has following properties:



ComboBoxCity.DataSource = ListOfCities  ' = List(Of City)
ComboBoxCity.ValueMember = "Id"
ComboBoxCity.DisplayMember = "Name"
ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))


My Problem



When DepartmentCustomerBindingSource.CurrentItemChanged occurs, the Combobox is not synchronized; it sometimes updates to the correct value, but when navigating further through the BindingSource it keeps the previous item selected



So, I have to do the following to update the combobox manually



Private Sub DepartmentCustomerBindingSource_CurrentItemChanged(sender As Object, e As EventArgs) Handles DepartmentCustomerBindingSource.CurrentItemChanged
If DepartmentCustomerBindingSource.Current.Contact.City Is Nothing then
ComboBoxCity.SelectedIndex = -1
Else
ComboBoxCity.SelectedItem = DepartmentCustomerBindingSource.Current.Contact.City
End if
End Sub


(for the sake of simplicity I have ommitted casting in my code above. Also these example classes might not make sense IRL)



EDIT



Even the code above does not do what I want.
For example: I have two instances of DepartmentCustomer, one with a Contact.City, the second without a Contact.City . When the form opens at the first instance, it shows the city; when I navigate to the second record, the combobox becomes empty, but when I go back to the first combobox, it stays empty; even more surprising, the first record has been updated to Contact.City = Nothing :'(



EDIT 2: My own not-preferred solution



I have removed the databinding from the combobox (ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))) and added the following sub



Private Sub ComboBoxCity_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBoxCity.SelectedValueChanged
DepartmentCustomerBindingSource.Current.Contact.City = ComboBoxCity.SelectedItem
End Sub


This works, but since I have quite a lot of these comboboxes in my form, I think there has to be an "automatic" way of synchronizing a two-way bound combobox...



Edit 3: I throw in the towel



Even my above "solution" is not working as expected; the Contact.City of the instances are not updated correctly when using the above code...



My Question



Why do I have to to this manually; Am I missing something? I would think that by setting the DataBinding, it would update the SelectedItem whenever the bound BindingSource is being navigated;










share|improve this question

























  • Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBinding​List not (correctly) implemented.

    – Marco Guignard
    Nov 15 '18 at 9:40











  • @MarcoGuignard thanks!!! see my answer below (and my additional question)

    – intrixius
    Nov 15 '18 at 10:01














2












2








2








My Setup



I have following (pseudo) EF classes:



class Department {
int ID,
string Name
}

class DepartmentCustomer {
int ID,
int CustomerID,
Customer Customer,
string Information,
int DepartmentID,
Department Department
}

Class Customer {
int ID,
string Name,
int? CityID,
City City
}

Class City{
int ID,
string Name,
string PostalCode
}


I have a form with a BindingSource for List(of DepartmentCustomer) and the following fields :





  • DepartmentCustomer.Information as a (read-write) textbox


  • DepartmentCustomer.Customer.Name as a (readonly) textbox


  • DepartmentCustomer.Customer.City as a (read-write) combobox.


CustomerForm



The combobox for the city name has following properties:



ComboBoxCity.DataSource = ListOfCities  ' = List(Of City)
ComboBoxCity.ValueMember = "Id"
ComboBoxCity.DisplayMember = "Name"
ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))


My Problem



When DepartmentCustomerBindingSource.CurrentItemChanged occurs, the Combobox is not synchronized; it sometimes updates to the correct value, but when navigating further through the BindingSource it keeps the previous item selected



So, I have to do the following to update the combobox manually



Private Sub DepartmentCustomerBindingSource_CurrentItemChanged(sender As Object, e As EventArgs) Handles DepartmentCustomerBindingSource.CurrentItemChanged
If DepartmentCustomerBindingSource.Current.Contact.City Is Nothing then
ComboBoxCity.SelectedIndex = -1
Else
ComboBoxCity.SelectedItem = DepartmentCustomerBindingSource.Current.Contact.City
End if
End Sub


(for the sake of simplicity I have ommitted casting in my code above. Also these example classes might not make sense IRL)



EDIT



Even the code above does not do what I want.
For example: I have two instances of DepartmentCustomer, one with a Contact.City, the second without a Contact.City . When the form opens at the first instance, it shows the city; when I navigate to the second record, the combobox becomes empty, but when I go back to the first combobox, it stays empty; even more surprising, the first record has been updated to Contact.City = Nothing :'(



EDIT 2: My own not-preferred solution



I have removed the databinding from the combobox (ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))) and added the following sub



Private Sub ComboBoxCity_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBoxCity.SelectedValueChanged
DepartmentCustomerBindingSource.Current.Contact.City = ComboBoxCity.SelectedItem
End Sub


This works, but since I have quite a lot of these comboboxes in my form, I think there has to be an "automatic" way of synchronizing a two-way bound combobox...



Edit 3: I throw in the towel



Even my above "solution" is not working as expected; the Contact.City of the instances are not updated correctly when using the above code...



My Question



Why do I have to to this manually; Am I missing something? I would think that by setting the DataBinding, it would update the SelectedItem whenever the bound BindingSource is being navigated;










share|improve this question
















My Setup



I have following (pseudo) EF classes:



class Department {
int ID,
string Name
}

class DepartmentCustomer {
int ID,
int CustomerID,
Customer Customer,
string Information,
int DepartmentID,
Department Department
}

Class Customer {
int ID,
string Name,
int? CityID,
City City
}

Class City{
int ID,
string Name,
string PostalCode
}


I have a form with a BindingSource for List(of DepartmentCustomer) and the following fields :





  • DepartmentCustomer.Information as a (read-write) textbox


  • DepartmentCustomer.Customer.Name as a (readonly) textbox


  • DepartmentCustomer.Customer.City as a (read-write) combobox.


CustomerForm



The combobox for the city name has following properties:



ComboBoxCity.DataSource = ListOfCities  ' = List(Of City)
ComboBoxCity.ValueMember = "Id"
ComboBoxCity.DisplayMember = "Name"
ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))


My Problem



When DepartmentCustomerBindingSource.CurrentItemChanged occurs, the Combobox is not synchronized; it sometimes updates to the correct value, but when navigating further through the BindingSource it keeps the previous item selected



So, I have to do the following to update the combobox manually



Private Sub DepartmentCustomerBindingSource_CurrentItemChanged(sender As Object, e As EventArgs) Handles DepartmentCustomerBindingSource.CurrentItemChanged
If DepartmentCustomerBindingSource.Current.Contact.City Is Nothing then
ComboBoxCity.SelectedIndex = -1
Else
ComboBoxCity.SelectedItem = DepartmentCustomerBindingSource.Current.Contact.City
End if
End Sub


(for the sake of simplicity I have ommitted casting in my code above. Also these example classes might not make sense IRL)



EDIT



Even the code above does not do what I want.
For example: I have two instances of DepartmentCustomer, one with a Contact.City, the second without a Contact.City . When the form opens at the first instance, it shows the city; when I navigate to the second record, the combobox becomes empty, but when I go back to the first combobox, it stays empty; even more surprising, the first record has been updated to Contact.City = Nothing :'(



EDIT 2: My own not-preferred solution



I have removed the databinding from the combobox (ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))) and added the following sub



Private Sub ComboBoxCity_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBoxCity.SelectedValueChanged
DepartmentCustomerBindingSource.Current.Contact.City = ComboBoxCity.SelectedItem
End Sub


This works, but since I have quite a lot of these comboboxes in my form, I think there has to be an "automatic" way of synchronizing a two-way bound combobox...



Edit 3: I throw in the towel



Even my above "solution" is not working as expected; the Contact.City of the instances are not updated correctly when using the above code...



My Question



Why do I have to to this manually; Am I missing something? I would think that by setting the DataBinding, it would update the SelectedItem whenever the bound BindingSource is being navigated;







vb.net winforms entity-framework-6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 9:26







intrixius

















asked Nov 15 '18 at 8:30









intrixiusintrixius

5251613




5251613













  • Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBinding​List not (correctly) implemented.

    – Marco Guignard
    Nov 15 '18 at 9:40











  • @MarcoGuignard thanks!!! see my answer below (and my additional question)

    – intrixius
    Nov 15 '18 at 10:01



















  • Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBinding​List not (correctly) implemented.

    – Marco Guignard
    Nov 15 '18 at 9:40











  • @MarcoGuignard thanks!!! see my answer below (and my additional question)

    – intrixius
    Nov 15 '18 at 10:01

















Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBinding​List not (correctly) implemented.

– Marco Guignard
Nov 15 '18 at 9:40





Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBinding​List not (correctly) implemented.

– Marco Guignard
Nov 15 '18 at 9:40













@MarcoGuignard thanks!!! see my answer below (and my additional question)

– intrixius
Nov 15 '18 at 10:01





@MarcoGuignard thanks!!! see my answer below (and my additional question)

– intrixius
Nov 15 '18 at 10:01












3 Answers
3






active

oldest

votes


















1














There seems to be an issue (bug?) with ComboBox data binding to SelectedItem property and null (Nothing) source values.



From the other side binding to SelectedValue has no such issue. So the solution/workaround is to bind to SelectedItem for updating the data soource and to SelectedValue for updating the control:



ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))    
ComboBoxCity.DataBindings.Add(New Binding("SelectedValue", DepartmentCustomerBindingSource, "Customer.City.ID", True, DataSourceUpdateMode.Never))





share|improve this answer
























  • Really? that would seem crazy. Any references perhaps?

    – intrixius
    Nov 15 '18 at 10:21











  • Reference? You have to be kidding :) Trial and error.

    – Ivan Stoev
    Nov 15 '18 at 10:46



















0















Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBinding​List not (correctly) implemented. – Marco Guignard




This got me thinking: I was indeed using the following to populate the DepartmentCustomerBindingSource



DepartmentCustomerBindingSource.DataSource = DBContext.DepartmentCustomer.Local.ToList


When changing that to



DepartmentCustomerBindingSource.DataSource = DBContext.DepartmentCustomer.Local.ToBindingList


it works :-/



However, how do I get this working when setting the datasource to a specific record instead of a (Binding)List?



DepartmentCustomerBindingSource.DataSource = DBContext.DepartmentCustomer.Local.First





share|improve this answer


























  • Add the implementation of INotifyPropertyChanged to the DepartmentCustomer Class docs.microsoft.com/en-us/dotnet/api/… By the way you better have to use ForeignKey Property to do the link between the selected value of your combo box

    – Marco Guignard
    Nov 15 '18 at 11:51











  • @MarcoGuignard I am clueless on why and how I have to do that. Does everybody who wants to use a Combobox to update a EF POCO have to implement this?

    – intrixius
    Nov 15 '18 at 14:56













  • On the comboBox, set the datasource to a collection Of City (a list is good enough if you don't update the list of city), set the DisplayMember to Name and the ValueMember to ID. Then set a binding between the SelectedValue of the combobox and the CityID property of your BindingSource containing your Customer.

    – Marco Guignard
    Nov 16 '18 at 9:48



















0














After a lot of trial-and-error I found the following solution:



Taking following code in consideration:



ComboBoxCity.DataSource = ListOfCities  ' = List(Of City)
ComboBoxCity.ValueMember = "Id"
ComboBoxCity.DisplayMember = "Name"
ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))


the thing that got me going was to set the ComboBoxCity.DataSource to a type that implements IBindingList, like following:



DBContext.Cities.Load
CitiesBindingSource.DataSource = DBContext.Cities.Local.ToBindingList
ComboBoxCity.DataSource = CitiesBindingSource


So, it was not the bound data (in this case DepartmentCustomerBindingSource) that had to be a BindingList, but only the Datasource had to implement IBindingSource...






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315194%2fwhy-do-i-need-to-update-a-bound-combobox-manually%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    There seems to be an issue (bug?) with ComboBox data binding to SelectedItem property and null (Nothing) source values.



    From the other side binding to SelectedValue has no such issue. So the solution/workaround is to bind to SelectedItem for updating the data soource and to SelectedValue for updating the control:



    ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))    
    ComboBoxCity.DataBindings.Add(New Binding("SelectedValue", DepartmentCustomerBindingSource, "Customer.City.ID", True, DataSourceUpdateMode.Never))





    share|improve this answer
























    • Really? that would seem crazy. Any references perhaps?

      – intrixius
      Nov 15 '18 at 10:21











    • Reference? You have to be kidding :) Trial and error.

      – Ivan Stoev
      Nov 15 '18 at 10:46
















    1














    There seems to be an issue (bug?) with ComboBox data binding to SelectedItem property and null (Nothing) source values.



    From the other side binding to SelectedValue has no such issue. So the solution/workaround is to bind to SelectedItem for updating the data soource and to SelectedValue for updating the control:



    ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))    
    ComboBoxCity.DataBindings.Add(New Binding("SelectedValue", DepartmentCustomerBindingSource, "Customer.City.ID", True, DataSourceUpdateMode.Never))





    share|improve this answer
























    • Really? that would seem crazy. Any references perhaps?

      – intrixius
      Nov 15 '18 at 10:21











    • Reference? You have to be kidding :) Trial and error.

      – Ivan Stoev
      Nov 15 '18 at 10:46














    1












    1








    1







    There seems to be an issue (bug?) with ComboBox data binding to SelectedItem property and null (Nothing) source values.



    From the other side binding to SelectedValue has no such issue. So the solution/workaround is to bind to SelectedItem for updating the data soource and to SelectedValue for updating the control:



    ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))    
    ComboBoxCity.DataBindings.Add(New Binding("SelectedValue", DepartmentCustomerBindingSource, "Customer.City.ID", True, DataSourceUpdateMode.Never))





    share|improve this answer













    There seems to be an issue (bug?) with ComboBox data binding to SelectedItem property and null (Nothing) source values.



    From the other side binding to SelectedValue has no such issue. So the solution/workaround is to bind to SelectedItem for updating the data soource and to SelectedValue for updating the control:



    ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))    
    ComboBoxCity.DataBindings.Add(New Binding("SelectedValue", DepartmentCustomerBindingSource, "Customer.City.ID", True, DataSourceUpdateMode.Never))






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 '18 at 10:19









    Ivan StoevIvan Stoev

    106k779129




    106k779129













    • Really? that would seem crazy. Any references perhaps?

      – intrixius
      Nov 15 '18 at 10:21











    • Reference? You have to be kidding :) Trial and error.

      – Ivan Stoev
      Nov 15 '18 at 10:46



















    • Really? that would seem crazy. Any references perhaps?

      – intrixius
      Nov 15 '18 at 10:21











    • Reference? You have to be kidding :) Trial and error.

      – Ivan Stoev
      Nov 15 '18 at 10:46

















    Really? that would seem crazy. Any references perhaps?

    – intrixius
    Nov 15 '18 at 10:21





    Really? that would seem crazy. Any references perhaps?

    – intrixius
    Nov 15 '18 at 10:21













    Reference? You have to be kidding :) Trial and error.

    – Ivan Stoev
    Nov 15 '18 at 10:46





    Reference? You have to be kidding :) Trial and error.

    – Ivan Stoev
    Nov 15 '18 at 10:46













    0















    Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBinding​List not (correctly) implemented. – Marco Guignard




    This got me thinking: I was indeed using the following to populate the DepartmentCustomerBindingSource



    DepartmentCustomerBindingSource.DataSource = DBContext.DepartmentCustomer.Local.ToList


    When changing that to



    DepartmentCustomerBindingSource.DataSource = DBContext.DepartmentCustomer.Local.ToBindingList


    it works :-/



    However, how do I get this working when setting the datasource to a specific record instead of a (Binding)List?



    DepartmentCustomerBindingSource.DataSource = DBContext.DepartmentCustomer.Local.First





    share|improve this answer


























    • Add the implementation of INotifyPropertyChanged to the DepartmentCustomer Class docs.microsoft.com/en-us/dotnet/api/… By the way you better have to use ForeignKey Property to do the link between the selected value of your combo box

      – Marco Guignard
      Nov 15 '18 at 11:51











    • @MarcoGuignard I am clueless on why and how I have to do that. Does everybody who wants to use a Combobox to update a EF POCO have to implement this?

      – intrixius
      Nov 15 '18 at 14:56













    • On the comboBox, set the datasource to a collection Of City (a list is good enough if you don't update the list of city), set the DisplayMember to Name and the ValueMember to ID. Then set a binding between the SelectedValue of the combobox and the CityID property of your BindingSource containing your Customer.

      – Marco Guignard
      Nov 16 '18 at 9:48
















    0















    Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBinding​List not (correctly) implemented. – Marco Guignard




    This got me thinking: I was indeed using the following to populate the DepartmentCustomerBindingSource



    DepartmentCustomerBindingSource.DataSource = DBContext.DepartmentCustomer.Local.ToList


    When changing that to



    DepartmentCustomerBindingSource.DataSource = DBContext.DepartmentCustomer.Local.ToBindingList


    it works :-/



    However, how do I get this working when setting the datasource to a specific record instead of a (Binding)List?



    DepartmentCustomerBindingSource.DataSource = DBContext.DepartmentCustomer.Local.First





    share|improve this answer


























    • Add the implementation of INotifyPropertyChanged to the DepartmentCustomer Class docs.microsoft.com/en-us/dotnet/api/… By the way you better have to use ForeignKey Property to do the link between the selected value of your combo box

      – Marco Guignard
      Nov 15 '18 at 11:51











    • @MarcoGuignard I am clueless on why and how I have to do that. Does everybody who wants to use a Combobox to update a EF POCO have to implement this?

      – intrixius
      Nov 15 '18 at 14:56













    • On the comboBox, set the datasource to a collection Of City (a list is good enough if you don't update the list of city), set the DisplayMember to Name and the ValueMember to ID. Then set a binding between the SelectedValue of the combobox and the CityID property of your BindingSource containing your Customer.

      – Marco Guignard
      Nov 16 '18 at 9:48














    0












    0








    0








    Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBinding​List not (correctly) implemented. – Marco Guignard




    This got me thinking: I was indeed using the following to populate the DepartmentCustomerBindingSource



    DepartmentCustomerBindingSource.DataSource = DBContext.DepartmentCustomer.Local.ToList


    When changing that to



    DepartmentCustomerBindingSource.DataSource = DBContext.DepartmentCustomer.Local.ToBindingList


    it works :-/



    However, how do I get this working when setting the datasource to a specific record instead of a (Binding)List?



    DepartmentCustomerBindingSource.DataSource = DBContext.DepartmentCustomer.Local.First





    share|improve this answer
















    Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBinding​List not (correctly) implemented. – Marco Guignard




    This got me thinking: I was indeed using the following to populate the DepartmentCustomerBindingSource



    DepartmentCustomerBindingSource.DataSource = DBContext.DepartmentCustomer.Local.ToList


    When changing that to



    DepartmentCustomerBindingSource.DataSource = DBContext.DepartmentCustomer.Local.ToBindingList


    it works :-/



    However, how do I get this working when setting the datasource to a specific record instead of a (Binding)List?



    DepartmentCustomerBindingSource.DataSource = DBContext.DepartmentCustomer.Local.First






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 15 '18 at 10:17

























    answered Nov 15 '18 at 10:01









    intrixiusintrixius

    5251613




    5251613













    • Add the implementation of INotifyPropertyChanged to the DepartmentCustomer Class docs.microsoft.com/en-us/dotnet/api/… By the way you better have to use ForeignKey Property to do the link between the selected value of your combo box

      – Marco Guignard
      Nov 15 '18 at 11:51











    • @MarcoGuignard I am clueless on why and how I have to do that. Does everybody who wants to use a Combobox to update a EF POCO have to implement this?

      – intrixius
      Nov 15 '18 at 14:56













    • On the comboBox, set the datasource to a collection Of City (a list is good enough if you don't update the list of city), set the DisplayMember to Name and the ValueMember to ID. Then set a binding between the SelectedValue of the combobox and the CityID property of your BindingSource containing your Customer.

      – Marco Guignard
      Nov 16 '18 at 9:48



















    • Add the implementation of INotifyPropertyChanged to the DepartmentCustomer Class docs.microsoft.com/en-us/dotnet/api/… By the way you better have to use ForeignKey Property to do the link between the selected value of your combo box

      – Marco Guignard
      Nov 15 '18 at 11:51











    • @MarcoGuignard I am clueless on why and how I have to do that. Does everybody who wants to use a Combobox to update a EF POCO have to implement this?

      – intrixius
      Nov 15 '18 at 14:56













    • On the comboBox, set the datasource to a collection Of City (a list is good enough if you don't update the list of city), set the DisplayMember to Name and the ValueMember to ID. Then set a binding between the SelectedValue of the combobox and the CityID property of your BindingSource containing your Customer.

      – Marco Guignard
      Nov 16 '18 at 9:48

















    Add the implementation of INotifyPropertyChanged to the DepartmentCustomer Class docs.microsoft.com/en-us/dotnet/api/… By the way you better have to use ForeignKey Property to do the link between the selected value of your combo box

    – Marco Guignard
    Nov 15 '18 at 11:51





    Add the implementation of INotifyPropertyChanged to the DepartmentCustomer Class docs.microsoft.com/en-us/dotnet/api/… By the way you better have to use ForeignKey Property to do the link between the selected value of your combo box

    – Marco Guignard
    Nov 15 '18 at 11:51













    @MarcoGuignard I am clueless on why and how I have to do that. Does everybody who wants to use a Combobox to update a EF POCO have to implement this?

    – intrixius
    Nov 15 '18 at 14:56







    @MarcoGuignard I am clueless on why and how I have to do that. Does everybody who wants to use a Combobox to update a EF POCO have to implement this?

    – intrixius
    Nov 15 '18 at 14:56















    On the comboBox, set the datasource to a collection Of City (a list is good enough if you don't update the list of city), set the DisplayMember to Name and the ValueMember to ID. Then set a binding between the SelectedValue of the combobox and the CityID property of your BindingSource containing your Customer.

    – Marco Guignard
    Nov 16 '18 at 9:48





    On the comboBox, set the datasource to a collection Of City (a list is good enough if you don't update the list of city), set the DisplayMember to Name and the ValueMember to ID. Then set a binding between the SelectedValue of the combobox and the CityID property of your BindingSource containing your Customer.

    – Marco Guignard
    Nov 16 '18 at 9:48











    0














    After a lot of trial-and-error I found the following solution:



    Taking following code in consideration:



    ComboBoxCity.DataSource = ListOfCities  ' = List(Of City)
    ComboBoxCity.ValueMember = "Id"
    ComboBoxCity.DisplayMember = "Name"
    ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))


    the thing that got me going was to set the ComboBoxCity.DataSource to a type that implements IBindingList, like following:



    DBContext.Cities.Load
    CitiesBindingSource.DataSource = DBContext.Cities.Local.ToBindingList
    ComboBoxCity.DataSource = CitiesBindingSource


    So, it was not the bound data (in this case DepartmentCustomerBindingSource) that had to be a BindingList, but only the Datasource had to implement IBindingSource...






    share|improve this answer




























      0














      After a lot of trial-and-error I found the following solution:



      Taking following code in consideration:



      ComboBoxCity.DataSource = ListOfCities  ' = List(Of City)
      ComboBoxCity.ValueMember = "Id"
      ComboBoxCity.DisplayMember = "Name"
      ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))


      the thing that got me going was to set the ComboBoxCity.DataSource to a type that implements IBindingList, like following:



      DBContext.Cities.Load
      CitiesBindingSource.DataSource = DBContext.Cities.Local.ToBindingList
      ComboBoxCity.DataSource = CitiesBindingSource


      So, it was not the bound data (in this case DepartmentCustomerBindingSource) that had to be a BindingList, but only the Datasource had to implement IBindingSource...






      share|improve this answer


























        0












        0








        0







        After a lot of trial-and-error I found the following solution:



        Taking following code in consideration:



        ComboBoxCity.DataSource = ListOfCities  ' = List(Of City)
        ComboBoxCity.ValueMember = "Id"
        ComboBoxCity.DisplayMember = "Name"
        ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))


        the thing that got me going was to set the ComboBoxCity.DataSource to a type that implements IBindingList, like following:



        DBContext.Cities.Load
        CitiesBindingSource.DataSource = DBContext.Cities.Local.ToBindingList
        ComboBoxCity.DataSource = CitiesBindingSource


        So, it was not the bound data (in this case DepartmentCustomerBindingSource) that had to be a BindingList, but only the Datasource had to implement IBindingSource...






        share|improve this answer













        After a lot of trial-and-error I found the following solution:



        Taking following code in consideration:



        ComboBoxCity.DataSource = ListOfCities  ' = List(Of City)
        ComboBoxCity.ValueMember = "Id"
        ComboBoxCity.DisplayMember = "Name"
        ComboBoxCity.DataBindings.Add(New Binding("SelectedItem", DepartmentCustomerBindingSource, "Customer.City", True, DataSourceUpdateMode.OnPropertyChanged))


        the thing that got me going was to set the ComboBoxCity.DataSource to a type that implements IBindingList, like following:



        DBContext.Cities.Load
        CitiesBindingSource.DataSource = DBContext.Cities.Local.ToBindingList
        ComboBoxCity.DataSource = CitiesBindingSource


        So, it was not the bound data (in this case DepartmentCustomerBindingSource) that had to be a BindingList, but only the Datasource had to implement IBindingSource...







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 7:48









        intrixiusintrixius

        5251613




        5251613






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315194%2fwhy-do-i-need-to-update-a-bound-combobox-manually%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine