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.
Step 2: Add data item->Click local property->Type (Integer) and uncheck Is Required->Ok.
Step 3: Go to CustomerID properties->Check Is Parameter.
Step 4: Add data item->Click query->Select Customer_SingleOrDefault->Ok.
Step 5: Go to Id properties->Select parameter binding (CustomerID).
Step 6: Go to write code->Select CreateNewCustomerEditInitializeDataWorkSpace.
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.
Step 8: Open customer table->properties->Select default screen (CreateNewCustomerEdit).
Step 9: Open search customer screen->Add..->Right click->Override code.
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.
Step 11: Now you can add and edit any records->Save.