Use of Percent Type in Light Switch 2012

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.

solution.jpg
 

Step 2

In the Solution Explorer, right-click on the data source and choose "Add Table".

data source add tbl.jpg
 

Step 3

The table appears.
 

student table.jpg
 

Step 4

In the table you can add a computed property to the Field by selecting the IsComputed property in the Property Window.

 is computed.jpg
 

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:

iscomputed table.jpg
 

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".

add src.jpg
 

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.

list and detail src.jpg
 

Step 9

The Screen Designer appears.

src designer.jpg
 

Step 10

Press F5 to run the application.

add new student output.jpg

Clicking the OK button we get:
 

application output1.jpg
 

Up Next
    Ebook Download
    View all
    Learn
    View all