Why do I need to update a bound combobox manually?
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.
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
add a comment |
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.
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
Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBindingList 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
add a comment |
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.
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
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.
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
vb.net winforms entity-framework-6
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 IBindingList 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
add a comment |
Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBindingList 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 IBindingList 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 IBindingList 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
add a comment |
3 Answers
3
active
oldest
votes
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))
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
add a comment |
Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBindingList 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
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 a comment |
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...
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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))
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
add a comment |
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))
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
add a comment |
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))
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))
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
add a comment |
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
add a comment |
Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBindingList 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
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 a comment |
Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBindingList 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
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 a comment |
Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBindingList 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
Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBindingList 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
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 a comment |
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
add a comment |
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...
add a comment |
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...
add a comment |
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...
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...
answered Nov 16 '18 at 7:48
intrixiusintrixius
5251613
5251613
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Most of time when a control doesn't update it's an issue related to the interfaces INotifyPropertyChanged and IBindingList 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