Mohamed Atia's Blog

Just another WordPress.com weblog

A GridView that gets data from LinqDataSource (Part 1) November 1, 2009

Filed under: ASP.NET,LINQ to SQL — Mohamed Atia's Blog @ 11:28 am
Tags: ,

Introduction: in this post I will discuss how to create a GridView whose data source is a LinqDataSource. The application will support the following features:

-         Paging, editing, deleting, and insertion of a new entry.

-         Using some AJAX controls AjaxControlToolkit such as CalenderExtender.

-         Validation using RegularExpressionValidator (inside the grid).

-         Cleaning up the foreign-key values & replacing them with their values (Binding UI with ASP LinqDataSource).

Part 1 Abstract:  you will expect in this part to know about:

-         Paging, editing, deleting.

Resources: Visual Studio 2008, SQL Server 2005 Express Edition

Prerequisites: Readers of this post must have a good knowledge about ASP.Net and LINQ to SQL, I will suppose that you know how to create and use Data Classes our .dbml file should look like this :

DBML

In this tutorial our data source is Microsoft Northwind Sample database we will show the Orders table in the GridView.

Let’s get started.

I just dragged and dropped a GridView from the ToolBox.

 Our_Start

Step 1: We need to Create a LinqDataSource, this can be done easily using the wizard as follows:

step1

Step 2: Select LINQ then press next

step2

Step 3: Make Sure that u selected the appropriate Data Classes

step3

Step 4: Select thr required table (in our case Orders) and click Advanced

step4

Step 5: Make sure that the Check Boxes are checked, this sets the properties of the LinqDataSource which are EnableDelete, EnableInsert, and EnableUpdate to true.

step5

Step 6: Make sure that the Grid enables paging, sorting, editing, and deletion. I am not concerned about selection in this article.

step6

Now let’s run the project and see what we get

what_have_we_done_1to6_1

If I click delete, the order will be deleted from the database :D , if I click update the following will happen:

what_have_we_done_1to6_2

Marvelous and simple!!

Update some values then click update it will reflect in the database.

What I have done is the simple part; I almost used Wizards and UI.

I haven’t yet completed writing this article but this is the end of this part.

Mohamed Atia
MCTS 

Advertisement
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.