locked
How to fill the textbox in a datagrid using the value of a local property automatically and make the ACB in this datagird automatically selected in a List and Detail screen when add a new child record? RRS feed

  • Question

  • Hi,

    I have a List and Detail screen. 

    I want  to use it to   only add a new record in the related chilidren Datagrid on the right pane.

    Because I only want to Add a new record and don't want to see the already existed other unrelated records.So I filtered the children entity which is shown in a datagrid to only show the record which is related to the item I selected in the left pane .

    Now, Everytime  I select an item in the left pane and  if there is no related record in the right pane. I will have to click the textbox in the datagrid to make it focused and then I will type a value in the textbox in the datagrid.   

    what I want is everytime when I click  the textbox in the datagrid, the text box will automatically be bound to a local property and the property's value will be shown in the textbox automatically. 

    By the way , there is also a ACB box in this data grid which has ONLY ONE value which is related to the item I selected in the left pane. And everytime I will have to mannually select it to make it shown in the ACB .

    With the textbox and the ACB mannually typed and selected, then I can save this record.

    How to fill in the textbox automatically with a local property and make the ACB's value automatically selected when I add a new record? 

    Thank you!


    • Edited by wanghu Saturday, May 3, 2014 1:47 AM typo
    Saturday, May 3, 2014 1:45 AM

All replies

  • It sounds to me like your data grid, in the right pane, is not bound correctly so it automatically updates to show the related records as you change the selected item in the list, in the left pane.

    If this is the situation, try looking at the query that is driving the data grid. If it takes a parameter, try binding that parameter to the relevant value of the List's SelectedItem.

    If I've misunderstood or you need more help, try posting screenshots of your screen (design and/or runtime) to explain some more.


    Simon Jones
    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.

    Monday, May 5, 2014 8:05 AM
  • Hi,

    My  second question can be simplified as how to set a default value in the autocomplete box in a datagrid, when the user add a new record in the datagrid ,without the user have to select a value in the acb manually. 

    Tuesday, May 6, 2014 10:54 AM
  • Don't think about manipulating the ACB.

    Use the entity code if possible.

    In the entity.Created event, set the default value for the property.

    EG

    Private Sub Company_Created()
        Me.Withdrawn = False
        Me.DoNotContact = False
     
        Me.Country = DataWorkspace.ProsperaData.Countries_SingleOrDefault("GB")
    End Sub

    The last line sets the default country for the new Company to be "United Kingdom". The ACB shows the data as normal.


    Simon Jones
    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.

    Tuesday, May 6, 2014 11:07 AM
  • Hi,

    I think the entity code is not possible in my situation. You can see from the left pane in the first image, one shelfsuppliersubjectitemindetails has many InventoryRukuItemlists. And I filtered the inventoryrukuitemlists query  to contain ONLY ONE value which should be bind to the shelfsuppliersubjectitemindetal record.

    And in the second image,you can see I have to manually select this ONLY ONE value to save the shelfsuppliersubjectitemindetails record.


    • Edited by wanghu Tuesday, May 6, 2014 12:53 PM typo
    Tuesday, May 6, 2014 12:52 PM
  • The filter that you mention is probably the key to this.

    In my example code I used the SingleOrDefault() method on the Countries collection to pick one country to assign as the default for the new Company because I happened to be picking by the primary key of the table.

    There's no reason you shouldn't use a Where method to code the filter to pick the one record you need to assign, finishing with a FirstOrDefault method to pick the first (only) matching record, or Nothing if there are no matches.

    EG, my example code could be expressed:

    Me.Country = DataWorkspace.ProsperaData.Countries.Where(Function(c) c.CountryCode= "GB").FirstOrDefault
    If the default value of one field depends on the value of a second, put the code in the Property_Changed event for the second field, checking to see if this second value has changed and if it is not Nothing before you try to use it to set the default.


    Simon Jones
    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.

    Tuesday, May 6, 2014 1:30 PM
  • Hi,

    I am new to LS.

    And I don't know how to write the Property_Changed event.

    I think I shoud write the following code .

     partial void ShelfSupplierSubjectitemInDetail_Created()
            {
               // Staff currentStaff = DataWorkspace.ApplicationData.Staffs.Where(staff => staff.LoginName == currentUser).FirstOrDefault();
                InventoryRukuItemList iril = DataWorkspace.ApplicationData.InventoryRukuItemLists.Where(a => a.Id == SomeId).FirstOrDefault();
               
            }

    But I don't know how to pass the value to the someId varialbe in 

    .Where(a => a.Id == SomeId).

    As you can see from the image, the InventoryRukuItemLists Query has two query paramters, these two parametes are bound to two local properties which are set as parameters and optional. These values are passed by clicking a command button  in another screen using this method

     partial void PutToShelf_Execute()
            {  
            this.Application.ShowCShelfSupplierSubjectItemsListDetail(this.InventoryRukuItemLists.SelectedItem.SupplierSubjectItem.Id, InventoryRukuItemLists.SelectedItem.InventoryRukuList.Id, this.InventoryRukuItemLists.SelectedItem.Quantity);
      
            }

    I don't know which Property should I write for the Property_changed method.

    If you need other information to help me out,let me know . I can email the entire source code to you if you like.

    Thank you!

    Tuesday, May 6, 2014 3:57 PM
  • To create a Property_Changed event, open the entity editor, select the property, click the Write Code dropdown and select the Property_Changed event. LightSwitch creates the event handler and takes you to it in the code editor.

    In your example code SomeID is just an integer variable. What you should set it to depends on how you expect this default to be determined. What should be the default  InventoryRukuItemList for this ShelfSupplierSubjectitemInDetail?


    Simon Jones
    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.

    Tuesday, May 6, 2014 5:05 PM
  • Hi, Simon

    Perhap I got what you mean. I think the followign code is what I should write for Property_Changed event.

     partial void Property2_Changed()
            {
                
                InventoryRukuItemList iril = DataWorkspace.ApplicationData.InventoryRukuItemLists.Where(a => a.SupplierSubjectItem.Id == Property1 && a.InventoryRukuList.Id == Property2).FirstOrDefault();
                //if (iril != null)
                //{
                //    this.ShelfSupplierSubjectitemInDetails.SelectedItem.InventoryRukuItemList = iril;
                //}
            }  

    But How can I pass the iril's Id property to the entity_created method?

     partial void ShelfSupplierSubjectitemInDetail_Created()
            {
               // Staff currentStaff = DataWorkspace.ApplicationData.Staffs.Where(staff => staff.LoginName == currentUser).FirstOrDefault();
                InventoryRukuItemList iril = DataWorkspace.ApplicationData.InventoryRukuItemLists.Where(a => a.Id == SomeId).FirstOrDefault();
               
            }

    Wednesday, May 7, 2014 12:24 AM
  • If the property you want to set the default for is not dependent on any other property, you would put all the code in the Entity_Created method.

    If the property's default depends on the value of another property you would put all the code in that (2nd) property's Property_Changed method.

    (Entity and Property here should be the names of your particular entity and property.)

    EG - The TaxRate on this Invoice line may be set by the TaxType, if the TaxType specifies a default TaxRate.

    Private Sub TaxType_Changed()
     
        If Me.Details.Properties.TaxType.IsChanged AndAlso
           TaxType IsNot Nothing AndAlso
           TaxType.TaxRate.HasValue Then
           TaxRate = TaxType.TaxRate
        End If
    
     End Sub
    
    Notice how we check in the Changed event to make sure the property really IsChanged before we do anything. This is because the Changed event is called on first loading the data from the database and we don't want to return the TaxRate to the default every time we open the record. That would wipe out the users changes and mark the record as dirty every time.


    Simon Jones
    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.

    Wednesday, May 7, 2014 6:42 AM
  • Hi,

    The following  code perhaps is what  I should write

    partial void Property2_Changed()
            {
                if (this.Details.Properties.Property2.IsChanged && (this.Details.Properties.Property2 != null))
                {
                    InventoryRukuItemList iril = DataWorkspace.ApplicationData.InventoryRukuItemLists.Where(a => a.SupplierSubjectItem.Id == Property1 && a.InventoryRukuList.Id == Property2).FirstOrDefault();
                    if (iril != null)
                    {
                       this.ShelfSupplierSubjectitemInDetails.SelectedItem.InventoryRukuItemList = iril;
                      
                    }
                }
            } 

    But when I debug , I got this.

    this.shelfsuppliersubjectitemindetails.selecteditem isnot there when I try to assign the IRIL entity to it.

     In my situation ,what variable should I assign the IRIL entity to?



    • Edited by wanghu Wednesday, May 7, 2014 1:43 PM typo
    Wednesday, May 7, 2014 12:34 PM