Create a screen which Edit and Add Records in a LightSwitch Application

Using this article one can easily create a screen that can both add and edit records in LightSwitch Application 2011.


Here you will learn step by step how to create a screen that can Edit and Add Records in a LightSwitch Application:

Step 1: Add screen->New data screen->Change screen name (CreateNewCustomerEdit)->Check Customer HeaderOrders->Ok.

image1.gif

Step 2: Add data item->Click local property->Type (Integer) and uncheck Is Required->Ok.

image2.gif

Step 3: Go to CustomerID properties->Check Is Parameter.

image3.gif

Step 4: Add data item->Click query->Select Customer_SingleOrDefault->Ok.

image4.gif

Step 5: Go to Id properties->Select parameter binding (CustomerID).

image5.gif

Step 6: Go to write code->Select CreateNewCustomerEditInitializeDataWorkSpace.

image6.gif

using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using System.Collections.Generic;
namespace LightSwitchApplication
{
    public partial class CreateNewCustomerEdit
    {
        partial void CreateNewCustomerEdit_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            // Write your code here.
            if (this.CustomerID.HasValue)
            {
                this.CustomerProperty = this.Customer;
            }
            else
            {
                this.CustomerProperty = new Customer();
            }
        }
        partial void CreateNewCustomerEdit_Saved()
        {
            // Write your code here.
            this.Close(false);
            Application.Current.ShowDefaultScreen(this.CustomerProperty);
        }
    }
}

Step 7: Save->Build client.

image7.gif

Step 8: Open customer table->properties->Select default screen (CreateNewCustomerEdit).

image8.gif

Step 9: Open search customer screen->Add..->Right click->Override code.

image9.gif

using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication
{
    public partial class SearchCustomers
    {
        partial void gridAddAndEditNew_CanExecute(ref bool result)
        {
            // Write your code here.
        }
        partial void gridAddAndEditNew_Execute()
        {
            // Write your code here.
            this.Application.ShowCreateNewCustomerEdit();
        }
    }
}

Step 10: Run application->Click + sign.

image10.gif

Step 11: Now you can add and edit any records->Save.

image11.gif