Use ComboBox as index in form - Update controls on ComboBox selection change










4















This is actually a very simple and basic question, and my apologies if this is a repeat question (surely it must be), but I can't find a straightforward answer anywhere and am finally throwing in the towel.



I have created a Windows Forms app in Visual Studio '17, created a data source based upon an SQL server table, and added a bound combobox. So far so good. Using the drag and drop feature from the DataSource/DataSet panel, I populate a few fields in text boxes and bind to data. Using the data binding navigator automatically added, I can scroll through the table and the controls all update, so I know they are all properly bound.



Now, all I want to accomplish is to have the controls update when the combobox selected value changes. But they do not. From what I've read, I'm guessing that somehow the combobox needs to alert that the datasource current record needs updating? If so, how do I get it to update to the key value from the combobox? Or maybe this is completely wrong? My hope was that VS would do its magic for bound controls so that I don't have to handle each combo change event, query the database, and then update each control.



I thought this would be a simple task; given that it's so very basic, surely there would be straight forward explanations, but either problems discussed are more complicated or else a labyrinth of data binding procs and objects are discussed. This is sort of a side venture from my usual .net and SQL coding, so I am hoping a straight forward event handler or two is all that's needed.










share|improve this question



















  • 1





    Try reading Brian Noyes' classic "Data Binding with Windows Forms 2.0", that should help...

    – Dean Kuga
    Nov 13 '18 at 20:43











  • Thank you for the suggestion, and I'm sure the book is very thorough. However, time does not allow me to study the ins and outs of .net data binding. But it's my very point that I should not have to dive into the framework in order to get the out-of-the-box functionality to work properly, especially for something so very simple. (Although it wouldn't be the first time!)

    – user3091705
    Nov 14 '18 at 19:04
















4















This is actually a very simple and basic question, and my apologies if this is a repeat question (surely it must be), but I can't find a straightforward answer anywhere and am finally throwing in the towel.



I have created a Windows Forms app in Visual Studio '17, created a data source based upon an SQL server table, and added a bound combobox. So far so good. Using the drag and drop feature from the DataSource/DataSet panel, I populate a few fields in text boxes and bind to data. Using the data binding navigator automatically added, I can scroll through the table and the controls all update, so I know they are all properly bound.



Now, all I want to accomplish is to have the controls update when the combobox selected value changes. But they do not. From what I've read, I'm guessing that somehow the combobox needs to alert that the datasource current record needs updating? If so, how do I get it to update to the key value from the combobox? Or maybe this is completely wrong? My hope was that VS would do its magic for bound controls so that I don't have to handle each combo change event, query the database, and then update each control.



I thought this would be a simple task; given that it's so very basic, surely there would be straight forward explanations, but either problems discussed are more complicated or else a labyrinth of data binding procs and objects are discussed. This is sort of a side venture from my usual .net and SQL coding, so I am hoping a straight forward event handler or two is all that's needed.










share|improve this question



















  • 1





    Try reading Brian Noyes' classic "Data Binding with Windows Forms 2.0", that should help...

    – Dean Kuga
    Nov 13 '18 at 20:43











  • Thank you for the suggestion, and I'm sure the book is very thorough. However, time does not allow me to study the ins and outs of .net data binding. But it's my very point that I should not have to dive into the framework in order to get the out-of-the-box functionality to work properly, especially for something so very simple. (Although it wouldn't be the first time!)

    – user3091705
    Nov 14 '18 at 19:04














4












4








4


0






This is actually a very simple and basic question, and my apologies if this is a repeat question (surely it must be), but I can't find a straightforward answer anywhere and am finally throwing in the towel.



I have created a Windows Forms app in Visual Studio '17, created a data source based upon an SQL server table, and added a bound combobox. So far so good. Using the drag and drop feature from the DataSource/DataSet panel, I populate a few fields in text boxes and bind to data. Using the data binding navigator automatically added, I can scroll through the table and the controls all update, so I know they are all properly bound.



Now, all I want to accomplish is to have the controls update when the combobox selected value changes. But they do not. From what I've read, I'm guessing that somehow the combobox needs to alert that the datasource current record needs updating? If so, how do I get it to update to the key value from the combobox? Or maybe this is completely wrong? My hope was that VS would do its magic for bound controls so that I don't have to handle each combo change event, query the database, and then update each control.



I thought this would be a simple task; given that it's so very basic, surely there would be straight forward explanations, but either problems discussed are more complicated or else a labyrinth of data binding procs and objects are discussed. This is sort of a side venture from my usual .net and SQL coding, so I am hoping a straight forward event handler or two is all that's needed.










share|improve this question
















This is actually a very simple and basic question, and my apologies if this is a repeat question (surely it must be), but I can't find a straightforward answer anywhere and am finally throwing in the towel.



I have created a Windows Forms app in Visual Studio '17, created a data source based upon an SQL server table, and added a bound combobox. So far so good. Using the drag and drop feature from the DataSource/DataSet panel, I populate a few fields in text boxes and bind to data. Using the data binding navigator automatically added, I can scroll through the table and the controls all update, so I know they are all properly bound.



Now, all I want to accomplish is to have the controls update when the combobox selected value changes. But they do not. From what I've read, I'm guessing that somehow the combobox needs to alert that the datasource current record needs updating? If so, how do I get it to update to the key value from the combobox? Or maybe this is completely wrong? My hope was that VS would do its magic for bound controls so that I don't have to handle each combo change event, query the database, and then update each control.



I thought this would be a simple task; given that it's so very basic, surely there would be straight forward explanations, but either problems discussed are more complicated or else a labyrinth of data binding procs and objects are discussed. This is sort of a side venture from my usual .net and SQL coding, so I am hoping a straight forward event handler or two is all that's needed.







c# .net winforms combobox






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 8 at 22:00









Reza Aghaei

65.8k854162




65.8k854162










asked Nov 13 '18 at 19:35









user3091705user3091705

928




928







  • 1





    Try reading Brian Noyes' classic "Data Binding with Windows Forms 2.0", that should help...

    – Dean Kuga
    Nov 13 '18 at 20:43











  • Thank you for the suggestion, and I'm sure the book is very thorough. However, time does not allow me to study the ins and outs of .net data binding. But it's my very point that I should not have to dive into the framework in order to get the out-of-the-box functionality to work properly, especially for something so very simple. (Although it wouldn't be the first time!)

    – user3091705
    Nov 14 '18 at 19:04













  • 1





    Try reading Brian Noyes' classic "Data Binding with Windows Forms 2.0", that should help...

    – Dean Kuga
    Nov 13 '18 at 20:43











  • Thank you for the suggestion, and I'm sure the book is very thorough. However, time does not allow me to study the ins and outs of .net data binding. But it's my very point that I should not have to dive into the framework in order to get the out-of-the-box functionality to work properly, especially for something so very simple. (Although it wouldn't be the first time!)

    – user3091705
    Nov 14 '18 at 19:04








1




1





Try reading Brian Noyes' classic "Data Binding with Windows Forms 2.0", that should help...

– Dean Kuga
Nov 13 '18 at 20:43





Try reading Brian Noyes' classic "Data Binding with Windows Forms 2.0", that should help...

– Dean Kuga
Nov 13 '18 at 20:43













Thank you for the suggestion, and I'm sure the book is very thorough. However, time does not allow me to study the ins and outs of .net data binding. But it's my very point that I should not have to dive into the framework in order to get the out-of-the-box functionality to work properly, especially for something so very simple. (Although it wouldn't be the first time!)

– user3091705
Nov 14 '18 at 19:04






Thank you for the suggestion, and I'm sure the book is very thorough. However, time does not allow me to study the ins and outs of .net data binding. But it's my very point that I should not have to dive into the framework in order to get the out-of-the-box functionality to work properly, especially for something so very simple. (Although it wouldn't be the first time!)

– user3091705
Nov 14 '18 at 19:04













1 Answer
1






active

oldest

votes


















1














When you want to setup a list control like ComboBox or ListBox to act as an index for data:



  • Set its DataSource property to the same DataSource to which other controls are bound.

  • Set its DisplayMember to show in combo box, but you don't need to set ValueMember.


  • Don't touch (DataBindings). You don't need to set up data-binding.

Example



Assuming you have a productsBindingSource, these are settings for bound controls:




  • idTextBox → Data Bindings: Text property bound to Id property of productsBindingSource


  • nameTextBox → Data Bindings: Name property bound to Name property of productsBindingSource


  • priceTextBox → Data Bindings: Price property bound to Price property of productsBindingSource

Then to have a ComboBox to act as index, these are the settings for ComboBox:




  • DataSource property set to productsBindingSource


  • DisplayMember property set to Name


  • Don't touch (DataBindings). You don't need to set up data-binding.

Then as a result, when you select an item from ComboBox, bound controls will show selected item and binding navigator will move as well:



enter image description here






share|improve this answer

























  • Reza, thank you, I got it to work. The only setting I altered was the combo box "ValueMember" (set to empty string), which is the key, apparently. If populated, it doesn't update the form, if empty, it does. I knew there had to be a simple answer since all the binding and navigating functionality is already there.

    – user3091705
    Nov 14 '18 at 19:24











  • You're welcome :). About ValueMember → You don't need to set it. It's enough to set DisplayMember to make it working. About (DataBindings) → You don't need to set them, not because the settings are there, because you really don't need to setup data-binding for a list control. In fact if you set ValueMember you will face with validation problem. What I mentioned in the answer are minimal required settings to make a list control to work as index. The ComboBox which you see in the gif image, is created based on the steps which I shared in the post.

    – Reza Aghaei
    Nov 14 '18 at 23:08












  • Actually, just getting back to this, you do, in fact, need to set "ValueMember" to the key field, which is often some "ID" field that you don't want to display, but that needs to be the basis upon which a row is selected from the data set. I found that without this set, the data list in the combo box becomes rapidly corrupted.

    – user3091705
    Jan 25 at 15:24











  • @user3091705 I double checked everything again. I can confirm again that you don't need to set ValueMember for a ComboBox which is just acting as index. Something like what you see in the gif animation which I shared. But setting the ValueMember is harmless as far as I see.

    – Reza Aghaei
    Jan 25 at 15:40










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%2f53288313%2fuse-combobox-as-index-in-form-update-controls-on-combobox-selection-change%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














When you want to setup a list control like ComboBox or ListBox to act as an index for data:



  • Set its DataSource property to the same DataSource to which other controls are bound.

  • Set its DisplayMember to show in combo box, but you don't need to set ValueMember.


  • Don't touch (DataBindings). You don't need to set up data-binding.

Example



Assuming you have a productsBindingSource, these are settings for bound controls:




  • idTextBox → Data Bindings: Text property bound to Id property of productsBindingSource


  • nameTextBox → Data Bindings: Name property bound to Name property of productsBindingSource


  • priceTextBox → Data Bindings: Price property bound to Price property of productsBindingSource

Then to have a ComboBox to act as index, these are the settings for ComboBox:




  • DataSource property set to productsBindingSource


  • DisplayMember property set to Name


  • Don't touch (DataBindings). You don't need to set up data-binding.

Then as a result, when you select an item from ComboBox, bound controls will show selected item and binding navigator will move as well:



enter image description here






share|improve this answer

























  • Reza, thank you, I got it to work. The only setting I altered was the combo box "ValueMember" (set to empty string), which is the key, apparently. If populated, it doesn't update the form, if empty, it does. I knew there had to be a simple answer since all the binding and navigating functionality is already there.

    – user3091705
    Nov 14 '18 at 19:24











  • You're welcome :). About ValueMember → You don't need to set it. It's enough to set DisplayMember to make it working. About (DataBindings) → You don't need to set them, not because the settings are there, because you really don't need to setup data-binding for a list control. In fact if you set ValueMember you will face with validation problem. What I mentioned in the answer are minimal required settings to make a list control to work as index. The ComboBox which you see in the gif image, is created based on the steps which I shared in the post.

    – Reza Aghaei
    Nov 14 '18 at 23:08












  • Actually, just getting back to this, you do, in fact, need to set "ValueMember" to the key field, which is often some "ID" field that you don't want to display, but that needs to be the basis upon which a row is selected from the data set. I found that without this set, the data list in the combo box becomes rapidly corrupted.

    – user3091705
    Jan 25 at 15:24











  • @user3091705 I double checked everything again. I can confirm again that you don't need to set ValueMember for a ComboBox which is just acting as index. Something like what you see in the gif animation which I shared. But setting the ValueMember is harmless as far as I see.

    – Reza Aghaei
    Jan 25 at 15:40















1














When you want to setup a list control like ComboBox or ListBox to act as an index for data:



  • Set its DataSource property to the same DataSource to which other controls are bound.

  • Set its DisplayMember to show in combo box, but you don't need to set ValueMember.


  • Don't touch (DataBindings). You don't need to set up data-binding.

Example



Assuming you have a productsBindingSource, these are settings for bound controls:




  • idTextBox → Data Bindings: Text property bound to Id property of productsBindingSource


  • nameTextBox → Data Bindings: Name property bound to Name property of productsBindingSource


  • priceTextBox → Data Bindings: Price property bound to Price property of productsBindingSource

Then to have a ComboBox to act as index, these are the settings for ComboBox:




  • DataSource property set to productsBindingSource


  • DisplayMember property set to Name


  • Don't touch (DataBindings). You don't need to set up data-binding.

Then as a result, when you select an item from ComboBox, bound controls will show selected item and binding navigator will move as well:



enter image description here






share|improve this answer

























  • Reza, thank you, I got it to work. The only setting I altered was the combo box "ValueMember" (set to empty string), which is the key, apparently. If populated, it doesn't update the form, if empty, it does. I knew there had to be a simple answer since all the binding and navigating functionality is already there.

    – user3091705
    Nov 14 '18 at 19:24











  • You're welcome :). About ValueMember → You don't need to set it. It's enough to set DisplayMember to make it working. About (DataBindings) → You don't need to set them, not because the settings are there, because you really don't need to setup data-binding for a list control. In fact if you set ValueMember you will face with validation problem. What I mentioned in the answer are minimal required settings to make a list control to work as index. The ComboBox which you see in the gif image, is created based on the steps which I shared in the post.

    – Reza Aghaei
    Nov 14 '18 at 23:08












  • Actually, just getting back to this, you do, in fact, need to set "ValueMember" to the key field, which is often some "ID" field that you don't want to display, but that needs to be the basis upon which a row is selected from the data set. I found that without this set, the data list in the combo box becomes rapidly corrupted.

    – user3091705
    Jan 25 at 15:24











  • @user3091705 I double checked everything again. I can confirm again that you don't need to set ValueMember for a ComboBox which is just acting as index. Something like what you see in the gif animation which I shared. But setting the ValueMember is harmless as far as I see.

    – Reza Aghaei
    Jan 25 at 15:40













1












1








1







When you want to setup a list control like ComboBox or ListBox to act as an index for data:



  • Set its DataSource property to the same DataSource to which other controls are bound.

  • Set its DisplayMember to show in combo box, but you don't need to set ValueMember.


  • Don't touch (DataBindings). You don't need to set up data-binding.

Example



Assuming you have a productsBindingSource, these are settings for bound controls:




  • idTextBox → Data Bindings: Text property bound to Id property of productsBindingSource


  • nameTextBox → Data Bindings: Name property bound to Name property of productsBindingSource


  • priceTextBox → Data Bindings: Price property bound to Price property of productsBindingSource

Then to have a ComboBox to act as index, these are the settings for ComboBox:




  • DataSource property set to productsBindingSource


  • DisplayMember property set to Name


  • Don't touch (DataBindings). You don't need to set up data-binding.

Then as a result, when you select an item from ComboBox, bound controls will show selected item and binding navigator will move as well:



enter image description here






share|improve this answer















When you want to setup a list control like ComboBox or ListBox to act as an index for data:



  • Set its DataSource property to the same DataSource to which other controls are bound.

  • Set its DisplayMember to show in combo box, but you don't need to set ValueMember.


  • Don't touch (DataBindings). You don't need to set up data-binding.

Example



Assuming you have a productsBindingSource, these are settings for bound controls:




  • idTextBox → Data Bindings: Text property bound to Id property of productsBindingSource


  • nameTextBox → Data Bindings: Name property bound to Name property of productsBindingSource


  • priceTextBox → Data Bindings: Price property bound to Price property of productsBindingSource

Then to have a ComboBox to act as index, these are the settings for ComboBox:




  • DataSource property set to productsBindingSource


  • DisplayMember property set to Name


  • Don't touch (DataBindings). You don't need to set up data-binding.

Then as a result, when you select an item from ComboBox, bound controls will show selected item and binding navigator will move as well:



enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 25 at 15:37

























answered Nov 14 '18 at 6:42









Reza AghaeiReza Aghaei

65.8k854162




65.8k854162












  • Reza, thank you, I got it to work. The only setting I altered was the combo box "ValueMember" (set to empty string), which is the key, apparently. If populated, it doesn't update the form, if empty, it does. I knew there had to be a simple answer since all the binding and navigating functionality is already there.

    – user3091705
    Nov 14 '18 at 19:24











  • You're welcome :). About ValueMember → You don't need to set it. It's enough to set DisplayMember to make it working. About (DataBindings) → You don't need to set them, not because the settings are there, because you really don't need to setup data-binding for a list control. In fact if you set ValueMember you will face with validation problem. What I mentioned in the answer are minimal required settings to make a list control to work as index. The ComboBox which you see in the gif image, is created based on the steps which I shared in the post.

    – Reza Aghaei
    Nov 14 '18 at 23:08












  • Actually, just getting back to this, you do, in fact, need to set "ValueMember" to the key field, which is often some "ID" field that you don't want to display, but that needs to be the basis upon which a row is selected from the data set. I found that without this set, the data list in the combo box becomes rapidly corrupted.

    – user3091705
    Jan 25 at 15:24











  • @user3091705 I double checked everything again. I can confirm again that you don't need to set ValueMember for a ComboBox which is just acting as index. Something like what you see in the gif animation which I shared. But setting the ValueMember is harmless as far as I see.

    – Reza Aghaei
    Jan 25 at 15:40

















  • Reza, thank you, I got it to work. The only setting I altered was the combo box "ValueMember" (set to empty string), which is the key, apparently. If populated, it doesn't update the form, if empty, it does. I knew there had to be a simple answer since all the binding and navigating functionality is already there.

    – user3091705
    Nov 14 '18 at 19:24











  • You're welcome :). About ValueMember → You don't need to set it. It's enough to set DisplayMember to make it working. About (DataBindings) → You don't need to set them, not because the settings are there, because you really don't need to setup data-binding for a list control. In fact if you set ValueMember you will face with validation problem. What I mentioned in the answer are minimal required settings to make a list control to work as index. The ComboBox which you see in the gif image, is created based on the steps which I shared in the post.

    – Reza Aghaei
    Nov 14 '18 at 23:08












  • Actually, just getting back to this, you do, in fact, need to set "ValueMember" to the key field, which is often some "ID" field that you don't want to display, but that needs to be the basis upon which a row is selected from the data set. I found that without this set, the data list in the combo box becomes rapidly corrupted.

    – user3091705
    Jan 25 at 15:24











  • @user3091705 I double checked everything again. I can confirm again that you don't need to set ValueMember for a ComboBox which is just acting as index. Something like what you see in the gif animation which I shared. But setting the ValueMember is harmless as far as I see.

    – Reza Aghaei
    Jan 25 at 15:40
















Reza, thank you, I got it to work. The only setting I altered was the combo box "ValueMember" (set to empty string), which is the key, apparently. If populated, it doesn't update the form, if empty, it does. I knew there had to be a simple answer since all the binding and navigating functionality is already there.

– user3091705
Nov 14 '18 at 19:24





Reza, thank you, I got it to work. The only setting I altered was the combo box "ValueMember" (set to empty string), which is the key, apparently. If populated, it doesn't update the form, if empty, it does. I knew there had to be a simple answer since all the binding and navigating functionality is already there.

– user3091705
Nov 14 '18 at 19:24













You're welcome :). About ValueMember → You don't need to set it. It's enough to set DisplayMember to make it working. About (DataBindings) → You don't need to set them, not because the settings are there, because you really don't need to setup data-binding for a list control. In fact if you set ValueMember you will face with validation problem. What I mentioned in the answer are minimal required settings to make a list control to work as index. The ComboBox which you see in the gif image, is created based on the steps which I shared in the post.

– Reza Aghaei
Nov 14 '18 at 23:08






You're welcome :). About ValueMember → You don't need to set it. It's enough to set DisplayMember to make it working. About (DataBindings) → You don't need to set them, not because the settings are there, because you really don't need to setup data-binding for a list control. In fact if you set ValueMember you will face with validation problem. What I mentioned in the answer are minimal required settings to make a list control to work as index. The ComboBox which you see in the gif image, is created based on the steps which I shared in the post.

– Reza Aghaei
Nov 14 '18 at 23:08














Actually, just getting back to this, you do, in fact, need to set "ValueMember" to the key field, which is often some "ID" field that you don't want to display, but that needs to be the basis upon which a row is selected from the data set. I found that without this set, the data list in the combo box becomes rapidly corrupted.

– user3091705
Jan 25 at 15:24





Actually, just getting back to this, you do, in fact, need to set "ValueMember" to the key field, which is often some "ID" field that you don't want to display, but that needs to be the basis upon which a row is selected from the data set. I found that without this set, the data list in the combo box becomes rapidly corrupted.

– user3091705
Jan 25 at 15:24













@user3091705 I double checked everything again. I can confirm again that you don't need to set ValueMember for a ComboBox which is just acting as index. Something like what you see in the gif animation which I shared. But setting the ValueMember is harmless as far as I see.

– Reza Aghaei
Jan 25 at 15:40





@user3091705 I double checked everything again. I can confirm again that you don't need to set ValueMember for a ComboBox which is just acting as index. Something like what you see in the gif animation which I shared. But setting the ValueMember is harmless as far as I see.

– Reza Aghaei
Jan 25 at 15:40

















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%2f53288313%2fuse-combobox-as-index-in-form-update-controls-on-combobox-selection-change%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

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党