none
Screen for One to One or Zero relationship in Visual Studio LightSwitch

    Question

  • Hi All,

    I have a one to zero or one relationship in LightSwitch. So when I make the screen for this, the child table fields are greyed out and I am not able to enter any data into it, How can solve this issue; Need your help Please!!!

    Monday, August 06, 2012 8:59 AM

Answers

  • Hi again,

    One to 0..1 structures are very powerful but should be handled with care. The solution is in the blog post I provided, but I'll try to rephrase things here.

    The problem with One to 0..1 relations is that the Lightswitch engine can not know what the intention of the user is: either create a child by default, either not creating a child. So, it's up to the user to decide if the child must be created or not. As a result it's perfectly normal that the child fields are greyed out, until the Parent.Child property isn't NEW-ed.

    I'll show you an example with 2 simple tables: PARENT (with one field called parentfield) and a CHILD table (with one field ChildField1). Furthermore, you'll never guess, they have a One to 0..1 relation.

    This is the screen layout:

    The clue is to attach a button (i called it CREATE, but you will find a better name) with following Command code:

    public partial class ParentsListDetail

    { partial void Create_Execute() { this.Parents.SelectedItem.Child = new Child(); } partial void Create_CanExecute(ref bool result) { result = this.Parents.SelectedItem.Child == null; } }

    Note, that the button is attached to a parent field. Suprisingly, if you attach it to  a child field it will not work !

    The above will result in following screen (You can for sure improve the design !)

    I hope this will help you.

    Good luck

    paul


    paul van bladel

    • Marked as answer by Inayat Rehman Tuesday, August 07, 2012 10:57 AM
    Tuesday, August 07, 2012 10:20 AM

All replies

  • Can you describe how you created the screen?

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

    Monday, August 06, 2012 9:18 AM
  • Thanks yann for your reply! I am making a ListandDetail screen for it. In which the chlids tables fields are also displayed with the master table fields. But the child table field are inactive and data cannot be put into it. Please tell me the way how to enable it....

    Inayat Rehman

    Monday, August 06, 2012 10:36 AM
  • Can anybody Please solve my problem. I am running out of time. My project deadline is 9 august. Please if somebody knows how to solve it, leave a reply, I am waiting for it.. Thanks in Advance...


    Inayat Rehman

    Tuesday, August 07, 2012 4:49 AM
  • I came accross the same problem long time ago.

    I wrote about it in this blog post: http://blog.pragmaswitch.com/?p=189 (I admit the post is a bit long)


    paul van bladel

    Tuesday, August 07, 2012 6:08 AM
  • I'd help you if I could, but I don't user 0..1-Many relationships, so I don't know why the child records are disabled.

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

    Tuesday, August 07, 2012 8:57 AM
  • Thank you Paul for your reply. Let me clear my problem to you. I have a master table and two child tables. The relationship between the master and chlid tables are 1:1,0. I made a ListandDetail screen for it. Now when I click on the screen, the master tables fields are enabled but the chlid table fields are disabled and I can't enter any data in to those.

    Hope you will give me a solution for it....


    Inayat Rehman

    Tuesday, August 07, 2012 9:37 AM
  • Thanks Yann for the reply, it's worth a alot for me.


    Inayat Rehman

    Tuesday, August 07, 2012 9:38 AM
  • Hi again,

    One to 0..1 structures are very powerful but should be handled with care. The solution is in the blog post I provided, but I'll try to rephrase things here.

    The problem with One to 0..1 relations is that the Lightswitch engine can not know what the intention of the user is: either create a child by default, either not creating a child. So, it's up to the user to decide if the child must be created or not. As a result it's perfectly normal that the child fields are greyed out, until the Parent.Child property isn't NEW-ed.

    I'll show you an example with 2 simple tables: PARENT (with one field called parentfield) and a CHILD table (with one field ChildField1). Furthermore, you'll never guess, they have a One to 0..1 relation.

    This is the screen layout:

    The clue is to attach a button (i called it CREATE, but you will find a better name) with following Command code:

    public partial class ParentsListDetail

    { partial void Create_Execute() { this.Parents.SelectedItem.Child = new Child(); } partial void Create_CanExecute(ref bool result) { result = this.Parents.SelectedItem.Child == null; } }

    Note, that the button is attached to a parent field. Suprisingly, if you attach it to  a child field it will not work !

    The above will result in following screen (You can for sure improve the design !)

    I hope this will help you.

    Good luck

    paul


    paul van bladel

    • Marked as answer by Inayat Rehman Tuesday, August 07, 2012 10:57 AM
    Tuesday, August 07, 2012 10:20 AM
  • Thank you very much paul. This is 100% solution for this issue...


    Inayat Rehman

    Tuesday, August 07, 2012 10:58 AM
  • You are welcome. I'm glad I could help you.



    paul van bladel

    Tuesday, August 07, 2012 11:09 AM
  • Dear Paul,

    Hope you are doing well. I have come across another problem and I am sure that you will provide me the solution for it. " I want to have a row counter in datagrid control of LightSwitch. I have master and detail screen, For the details I want to have a rows counter which will start from 1 to the end of the datagrid row. Like if I have 10 records for a master entry in the datagrid then the rowcounter should start from 1 and end at 10.

    Waiting for your reply!!

    Best Regards,


    Inayat Rehman

    Thursday, August 09, 2012 4:54 AM
  • Hi again,

    I have googled this for you on the Lightswitch forum and I believe that the following link can help you:

    http://social.msdn.microsoft.com/Forums/en-AU/lightswitch/thread/d2b97cf0-db19-40e3-82fa-2e792334a3c2

    If not, it's best to create a new forum post because it's a different subject.


    paul van bladel

    Thursday, August 09, 2012 6:22 AM
  • Hello Inayat,

    I'd like to send me how you could solve this case, since I have the same situation. I have a Customer table and two child tables named Person and Group relazion with a zero or one-to-one relationship, because depending on the type of customer records data on one or the other.

    Thank you.
    Wednesday, September 19, 2012 2:48 AM