In this article I describe the use of the percent
datatype in LightSwitch using Visual Studio 2012.
The following is the procedure for using the
percent type in LightSwitch 2012.
Step 1
Open the Solution Explorer.
Step 2
In the Solution Explorer, right-click on the data
source and choose "Add Table".
Step 3
The table appears.
Step 4
In the table you can add a computed property to the Field by
selecting the IsComputed property in the Property Window.
Follow the same process for the remaining two fields (in other
words for Fail and Undecided fields) in order to add a computed property.
Step 5
Now the table appears in the following manner:
Step 6
Go to the Menu Bar under the write code option. Select the
_Compute method for the three fields in other words for the , Fail and
Undecided fields.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
Microsoft.LightSwitch;
namespace
LightSwitchApplication
{
public
partial
class
Student
{
private
decimal
totalmarks
{
get
{
decimal
totalmarks = 0;
if
((this.Student
!=
null)
&& (this.FailStudent
!=
null)
&& (this.UndecidedStudent
!=
null))
{
totalmarks = (decimal)(this.Student
+
this.FailStudent
+
this.UndecidedStudent);
}
return
totalmarks;
}
}
partial
void
_Compute(ref
decimal?
result)
{
// Set result to the desired field value
if
(totalmarks > 0)
{
result =
Decimal.Round((decimal)this.Student
/ totalmarks, 5);
}
else
{
result = 0;
}
}
partial
void
Fail_Compute(ref
decimal?
result)
{
// Set result to the desired field value
if
(totalmarks > 0)
{
result =
Decimal.Round((decimal)this.FailStudent
/ totalmarks, 5);
}
else
{
result = 0;
}
}
partial
void
Undecided_Compute(ref
decimal?
result)
{
// Set result to the desired field value
if
(totalmarks > 0)
{
result =
Decimal.Round((decimal)this.UndecidedStudent
/ totalmarks, 5);
}
else
{
result = 0;
}
}
}
}
Step 7
Right-click on the Screens and choose "Add Screen".
Step 8
Select the List and Details Screen from the Screen Template.
Under Screen Information we provide the Screen Name and Screen Data and then
click OK.
Step 9
The Screen Designer appears.
Step 10
Press F5 to run the application.
Clicking the OK button we get: