dcsimg
Results 1 to 14 of 14

Thread: Select a database row via a combobox?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2018
    Posts
    31

    Question Select a database row via a combobox?

    I have a combobox dragged from my datatable on my form along with the other parts of the table. On formload, I am loading the combobox with entries saved to the table/database.
    When I select an entry in the combobox, I want it to select the corresponding row and fill in the entries.
    I have done a lot of Googling and found lots of answers that don't work and/or have errors.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,984

    Re: Select a database row via a combobox?

    Your description is a little unclear because I would have expected what you're asking for to be happening already. Please describe EXACTLY what you did and EXACTLY what the result was.

    In general, you should bind your controls to a BindingSource and bind that to the DataSet. If you dragged a table or column from the Data Sources window onto the form then it should have automatically created a DataSet, a table adapter and a BindingSource, as well as the control(s).

    The DataSource property of the BindingSource should be set to the DataSet and the DataMember property should be set to the DataTable of interest. For the ComboBox, the DataSource should be set to the BindingSource and the DisplayMember should be set to the column you want to display. You may also set the ValueMember to the primary key column but that's probably not too important in this case.

    For controls like TextBoxes, you need to look at the (DataBindings) node in the Properties window. You first need to select the property you want to bind, which will generally be the Text property. Again, you need to select the BindingSource and the column you want displayed.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2018
    Posts
    31

    Re: Select a database row via a combobox?

    Name:  scrnsht.jpg
Views: 60
Size:  18.6 KB
    Like I said in another post, I can't get my head wrapped around databases.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2018
    Posts
    31

    Re: Select a database row via a combobox?

    Name:  scrnsht.jpg
Views: 60
Size:  18.6 KB
    Like I said in another post, I can't get my head wrapped around databases.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,984

    Re: Select a database row via a combobox?

    When I said:
    Quote Originally Posted by jmcilhinney View Post
    Please describe EXACTLY what you did and EXACTLY what the result was.
    I didn't mean post a screenshot that is too small to read anything and would provide only part of the story anyway.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2018
    Posts
    31

    Re: Select a database row via a combobox?

    Sorry, I had pc issues cause a double post of an incomplete post. IDK.
    Anyway, the comboboxes were dragged from the data sources window, but they don't populate unless loaded during startup of the app (for each ....).
    When I select an item in the combobox dropdown, nothing happens.
    I've tried various codes found online in the combobox.selectedindexchanged event, and still it doesn't work.
    Something possibly connected, is the fact that nothing saves in the database when I click on the "Save" button in the TblContactsBindingNavigator. I will be reading the related link in your signature reguarding that.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2018
    Posts
    31

    Re: Select a database row via a combobox?

    Sorry, I had pc issues cause a double post of an incomplete post. IDK.
    Anyway, the comboboxes were dragged from the data sources window, but they don't populate unless loaded during startup of the app (for each ....).
    When I select an item in the combobox dropdown, nothing happens.
    I've tried various codes found online in the combobox.selectedindexchanged event, and still it doesn't work.
    Something possibly connected, is the fact that nothing saves in the database when I click on the "Save" button in the TblContactsBindingNavigator. I will be reading the related link in your signature reguarding that.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,984

    Re: Select a database row via a combobox?

    For future reference, there is an issue with the site at the moment where, under certain circumstances, it will try to submit your post twice. The second submission will fail though, and you will be told that you need to wait 29 seconds or thereabouts to post again. If you get that message then, unless you have submitted two separate posts in quick succession, don't try to post again. As you can see, your last two posts and been duplicated as a result.

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,984

    Re: Select a database row via a combobox?

    As for the issue, you should be bind the controls as I have already explained. You don't need any code to populate the ComboBox other than calling Fill on your table adapter. That will query the database and populate the appropriate DataTable with the result set. If that DataTable is bound to the ComboBox, as it should be, then it will automatically display the data. So, check whether things are bound properly and, if they're not, bind them. Read post #2 and set the appropriate properties as I instructed.

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,984

    Re: Select a database row via a combobox?

    I generally don't drag anything from the Data Sources window so I don't know what the exact behaviour is when you drag a ComboBox but I suspect that you'll find that the SelectedValue is bound. In this case, that's not what you want so you should clear that. You can do so from the (DataBindings) node in the Properties window. Once that's done, bind it as I said earlier, i.e. by setting the DatatSource and DisplayMember.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Apr 2018
    Posts
    31

    Re: Select a database row via a combobox?

    Ok, I got it. Thank you.
    Now about saving... The link in your signature is not working (404).

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,984

    Re: Select a database row via a combobox?

    Quote Originally Posted by Amerigoware View Post
    The link in your signature is not working (404).
    Which link? There's more than one and the most likely candidates appear to be working for me. Maybe that particular server was just offline temporarily when you tried it.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Apr 2018
    Posts
    31

    Re: Select a database row via a combobox?

    The first one. "Why is my data not saved to my database?" (https://docs.microsoft.com/en-us/vis...t?view=vs-2015)

  14. #14
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,984

    Re: Select a database row via a combobox?

    Hmmm... can't find a new location for that content right now either. I've been meaning to write a blog post on that subject for a while so maybe now is the time. I'm not sure that's necessarily too relevant though. That link is basically about the locations of data files in a project and making sure that you're saving to the right one. It wasn't about how to save data. For that, you might try the CodeBank link in my signature and check out my thread on Retrieving & Saving Data. That's off the topic of this thread though, so you should probably start a new thread if you have further questions on that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width