Tuesday 7 December 2010

Beginners Guide to Visual Studio LightSwitch (Part - 3)

Visual Studio LightSwitch is a new tool for building data-driven Silverlight Application using Visual Studio IDE. It automatically generates the User Interface for a DataSource without writing any code. You can write a small amount of code also to meet your requirement.
In my previous chapter “Beginners Guide to Visual Studio LightSwitch (Part – 2)”, I described you how to create a search record window & export the records to Excel Sheet using Visual Studio LightSwitch for Silverlight. I also demonstrated about sorting and navigating records without writing a single line of code.
In this chapter, I will guide you step-by-step to create a DataGrid of records. Here you will know, how to insert/modify/delete records. These all steps are without writing any code. We will use just the tool to improve our existing application.

Background

If you are new to Visual Studio LightSwitch, I will first ask you to read the first two chapter of this tutorial, where I demonstrated it in detail. In my 2nd chapter, I discussed the following topics:
  • Create the Search Screen
  • See the Application in Action
  • Customizing the Search Screen
  • Sorting the Records
  • Customize the Name of the Screen
  • Navigation and Export to Excel
In this Chapter, we will discuss on the “Editable Data Grid” screen. Read it and learn more about this tool before it get release.

Create the Editable DataGrid Screen

Open the solution “HelloLightSwitchDemo1” that we created in our first chapter. From the Solution Explorer, double click to open the “StudentTables” as shown in the below figure:
image
This will open the table in design view. Click on the button called “Screen” to create a screen based on the entity.
image
This will open up the “Add New Screen” dialog in the screen. From the dialog shown below, select the “Editable Grid Screen” from the left panel. This will show the preview in the middle panel. The editable grid screen can be use to edit one or more records inside a datagrid.
image
From the right panel, select the Screen Data i.e. the table you want to work with.
image
This will set the ScreenName in the same dialog. You can change it too here or can change it later as discussed in the earlier chapters. Currently, I am going with the default name. Click “OK” to continue creating the screen.
image
Once you click “OK”, it will close the dialog and open the designer window of the screen inside Visual Studio IDE. You can change something here. If you read the earlier chapters, you will familiar with this screen. Currently I am going with the default screen because I want you should understand it easily.
image
Once everything is done, run the application to see the screen in action.

See the Application in Action

Now, it’s time to run the application to see the implemented screen. To do this, press F5. This will build the project and run the Silverlight application outside browser window (Out-of-Browser application). You will see the following UI once it run properly:
image
The default screen here will be the “New Student” tab. Close the tab and from the left panel click on the menu item called “EditableStudentTableGrid”.
image
It will open the tab ain to the screen s shown below:
image
You can see that, the screen has one search textbox panel which actually filters the records. It has a command button panel to do Add, Edit, Delete and Refresh function. It has a button called “Export to Excel”. It exports the record to the excel application sheet. It has one navigation panel to paginate the records. Let’s discuss them one by one.

Edit a Record

Let us start discussing on how to edit a record first. I will show two different approach. Select any record and double click on the column that you want to modify. You will see that, the cell will become in editable mode and the value present in the cell will be selected automatically.
image
Now set a different value to the cell. Press TAB to modify another cell.
image
Once you done modifying your records, press the “Save” button placed at the top left corner of the screen. You can also press “CTRL + S” to save the modified records.
image
Now let’s see another approach to edit a record. Select the row that you want to modify and click on the button called “Edit…”.
image
This will bring a child window with all the fields related to the record. The input boxes will be auto field with the data records. Modify the fields that you want and click “OK”.
image
Now press “Save” to update the records in the database table.

Create a New Record

We just completed editing the records. So, what next? Let’s create a new record from this screen. Here also, I will show you two different approach for inserting a new record.
image
In the first approach, click on a empty row and it will create a new row in the DataGrid as shown in the above UI. Start entering some records in the Grid.
image
Fill up all the fields and press the Save button present at the top left corner of the screen. This will save the records to the table.
image
Now we will show the same step using the second approach. Click on the “Add…” button as shown in the below screen:
image
This will popup the Child Window on top of the screen. It will have all the fields present in the table. Enter all the records.
image
Click “OK” and in the parent screen, press “Save” to store the new record. You will see the new records in the DataGrid.

Validate the Record

Before doing anything, I will show you the validation in the same screen. We will enter empty record in the screen and press “Save” button now. See the following screenshots:
image
You will see that, it actually validates the record automatically and show a message to the screen showing “Please correct data entry errors and try to save again.” Click “OK” to go back to the parent screen.
image
You will notice that it actually mark those fields as “Red border”.
image
Fill those mandatory fields and click “Save” once again. You will notice that, those validation error will go out from the screen and the new records will get saved in to the table.

Delete a Record

We had gone to a lengthy discussion of inserting and modifying records. Now it’s time to delete a record. Select the record(s) and press delete. You will notice that, the record has been deleted from the DataGrid.
image
Now go with the second approach. It is the same thing that we saw in the first approach. Select the row(s) and click the “Delete…” button in the screen (as shown in the below screenshot).
image
You will see that, the record has been deleted from the DataGrid and there is a red “x” mark placed at the side of the record. That means, the record has been deleted.
image
Press “Save”. You will see the record completely removed from the record table.
image
Woo, what a simple approach. No code, no XAML. The application is ready with add, edit and delete functionality. Let’s see some more features of it.

Filter & Export Records

This step we already discussed in the 2nd Chapter. But we will discuss it once again. In the same screen you can sort the records very easily. If you want to sort them by “Age”, click on the header of the column and you will see the records automatically sorted.
image
To filter out the records, start typing your query key in the search panel. You will notice that, the record has been filtered properly as per the search key. Have a look in to the following snapshot:
image
Oh yea, you can also export the records to an Excel worksheet by just pressing a single button. Click on “Export to Excel” button and you will watch the records exported to a Excel workbook. Yes, there’s a condition. You must have Excel installed in your PC. Winking smile
image
There’s also a Navigation panel to paginate all the records.

Customizing Screen

You can also customize the screen. You can change the title of the screen. I will not discuss more on this topic. Read the first two chapters. I discussed more about this point there. To change the screen name, click on the “Customize Screen” button placed at the right corner of the screen.
image
Once you update the display name there, save the settings and restart the application to see the updated settings in the screen.
image
That’s all about it. You will now see that the new name has been assigned to the screen.

No comments:

Post a Comment

what is Juice Jacking SCAM

  Juice Jacking is a cybersecurity threat that occurs when cybercriminals manipulate public charging stations, such as USB charging ports in...