locked
Dynamically change a dropdown box source RRS feed

  • Question

  • Hi

    I've got a Data Grid Row in Lightswitch that displays a list of questions (like a survey).

    Each question's have a limited list of possible answers, but the possible answers for each question is different.

    I would like to display the possible answers in a drop down box and then the drop down box must requery it's contents for each question. 

    Is it possible and how do I do that?

    Wednesday, April 18, 2012 11:37 AM

Answers

  • Okay... if you wanted to use an Editable Grid screen you could probably do that with the drop-down control populated for each question.  Follow the same general approach as before except rather than adding just a single item to the screen for Questions, you're creating a grid full of questions.  The databinding for the Query has to be subtly different in response.  Set the QuestionId parameter to Questions.SelectedItem.Id instead of Question.Id, then you should be off to the races.

    Be warned though... there might be some value in paging your list of questions if you have a lot of them.  This is because LightSwitch will basically do a "foreach" operation behind the scenes to populate the dropdown list.  This means that it will re-run the query for each grid row with a different Question.SelectedItem.Id value for each row.

    Do you know how to configure paging on your base Query items?

    Cheers


    Jeremy Huppatz
    Managing Consultant
    Solitaire Systems

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    • Proposed as answer by Jeremy Huppatz Thursday, April 19, 2012 7:59 AM
    • Marked as answer by Jowalski Thursday, April 19, 2012 8:56 AM
    Thursday, April 19, 2012 7:59 AM
  • I did find an answer, and the trick is to create a query with a parameter and add it to your form.
    The parameter should then be bound to Questions.SelectedItem.Question.

    The Choices property of the dropdown can then point to the query.

    • Marked as answer by Jowalski Monday, May 14, 2012 9:37 AM
    Monday, May 14, 2012 9:37 AM

All replies

  • Hi,

    Maybe you should read this article about cascading dropdown lists by Beth massi.


    Jan D'Hondt - Database and .NET development

    Wednesday, April 18, 2012 1:45 PM
  • This should be pretty simple assuming you're using a Details screen with navigation buttons as the basis for each question.

    If you have (for example) a Questions table related 1:M to an AnswerOptions table, you could solve this problem as follows:

    1. Create a Query over your AnswerOptions table that filters on QuestionId and name it AnswerOptionsFilterQuery
    2. Create a details screen based on the Questions table
    3. Click on the greyish "+AnswerOptions" item under Questions - Selected Item in the left panel of your designer to link the AnswerOptions table to the Question property of the screen
    4. Add the Question property of the screen to the designer and select the properties to be displayed and appropriate formatting
    5. Add the AnswerOptions entity to the designer and select the desired control, fields and layout
    6. Add the AnswerOptionsFilterQuery to your detail screen as a data query item
    7. Click on the AnswerOptions item in your screen designer and look at the properties tab.  There should be an item called "Choices" which has a default value of "Auto".  If you drop down the list of options, your AnswerOptionsFilterQuery should be available as an option in the list.  Select this.
    8. Now, add the navigation buttons to select the next question record/previous question record or to navigate to a screen that displays the full list of questions in a grid with links to the respective details screens
    9. Finally, add code-behind to do the navigation (simplest option for next/previous is to probably along the linest of incrementing/decrementing the Id property of the Question then call Refresh())

    That's it.  Your screen should now update the AnswerOptions controls in response to the change of Question Id. :)

    HTH


    Jeremy Huppatz
    Managing Consultant
    Solitaire Systems

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    • Proposed as answer by Jeremy Huppatz Thursday, April 19, 2012 6:49 AM
    • Unproposed as answer by Jeremy Huppatz Thursday, April 19, 2012 7:59 AM
    Thursday, April 19, 2012 6:27 AM
  • Hi Jeremy

    Thanks a lot for the answer.  If I understand correctly, then each screen will only show 1 question, correct? The navigation will then be used to move on to the next question.

    Thursday, April 19, 2012 7:21 AM
  • That's how my scenario above works, yes.  If you want something different to that, feel free to add more detail about how you're currently trying to lay out the screen.


    Jeremy Huppatz
    Managing Consultant
    Solitaire Systems

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    Thursday, April 19, 2012 7:38 AM
  • OK - thanks.

    I'm not saying that the 1 page per question will not work for me, but there are quite a lot of questions - perhaps 30 to 50, so it might be a bit cumbersome.

    I do not have a screen layout currently, so any layout can work.  

    I just have a table with the Questions that should be answered, and another table with the possible Answers per question.  I was hoping on getting all the questions on 1 screen, and then the dropdown box with the answers can change its RecordSource dynamically (like in the Access OnCurrent event).

    Thursday, April 19, 2012 7:50 AM
  • Okay... if you wanted to use an Editable Grid screen you could probably do that with the drop-down control populated for each question.  Follow the same general approach as before except rather than adding just a single item to the screen for Questions, you're creating a grid full of questions.  The databinding for the Query has to be subtly different in response.  Set the QuestionId parameter to Questions.SelectedItem.Id instead of Question.Id, then you should be off to the races.

    Be warned though... there might be some value in paging your list of questions if you have a lot of them.  This is because LightSwitch will basically do a "foreach" operation behind the scenes to populate the dropdown list.  This means that it will re-run the query for each grid row with a different Question.SelectedItem.Id value for each row.

    Do you know how to configure paging on your base Query items?

    Cheers


    Jeremy Huppatz
    Managing Consultant
    Solitaire Systems

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    • Proposed as answer by Jeremy Huppatz Thursday, April 19, 2012 7:59 AM
    • Marked as answer by Jowalski Thursday, April 19, 2012 8:56 AM
    Thursday, April 19, 2012 7:59 AM
  • Thanks - will try it.

    Thursday, April 19, 2012 8:56 AM
  • You put me on the right track, so thanks a lot.  Appreciated.

    I now have a editable grid that shows the questions, and a drop down box for the answers, but the dropdown box does not yet show the correct content.

    I have a Query (based on the possible answers) that takes a Parameter called QuestionID.  This query is added to my screen and the parameter points to a "screen parameter" called ScreenQuestionID.  I now need to tell ScreenQuestionID to get the value of the active question, but now sure how to do that.  Any pointers again?

    Friday, April 20, 2012 1:26 PM
  • Just a quick one...

    I tried doing a test mockup of the solution I proposed and it doesn't seem to be workable for the Editable Grid scenario.  The best I can achieve is to list the responses in a List Control within a grid and add a button to save the responses to it, or a drop-down list that renders multiple times, but is actually the same control - so if you update the selected item in the Question grid, you're effectively changing the control for all grid rows.

    I think this might be a limitation on the controls available.  I haven't seen anything that quite lets you get around this.  There's a demo project for a custom control called the "Filter Control" out there, but I haven't been able to figure out exactly how to make this work as the documentation for it seems pretty light on in the "How To" department.

    MS Peeps...

    Is there a property on the AutoComplete control that I could update to change the instance scope from the grid rows collection to just a single row in such a way that each instance of the control operates independently of all others?


    Jeremy Huppatz
    Managing Consultant
    Solitaire Systems

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    Saturday, April 21, 2012 4:50 PM
  • Thanks for the trouble.

    I did manage to get the FilterControl in my app and working (sort of), but doesn't seem that it will be of any assistance.
    Can you perhaps send me your sample with the Drop Down box that renders multiple times? Can try: Andre.Williams@myself.com.

    Monday, April 23, 2012 10:04 AM
  • I did find an answer, and the trick is to create a query with a parameter and add it to your form.
    The parameter should then be bound to Questions.SelectedItem.Question.

    The Choices property of the dropdown can then point to the query.

    • Marked as answer by Jowalski Monday, May 14, 2012 9:37 AM
    Monday, May 14, 2012 9:37 AM