CRUD Operation With GridView Using LINQ And Lambda Expressions

Introduction

As we all know about GridView, it is a Data Control of ASP.NET which is used to display sata in tabular format.

Lambda Expression

Lambda expression facilitates functional programming. It’s an Anonymous Function.

Syntax:

(Input=>Expression )

Features:

  • Using Anonyms variable/parameters.
  • No need to define scope like curly braces.
  • No need to return.

LINQ Query (Language Integrated Query):

In simple terms, LINQ provide us the facility to interact from front end to databases without directly using and ADO.NET Classes.

In other words, LINQ is a collection of Extension Methods.

Syntax:

From<range variable> IN <IEnumerableCollection >

The following are the basic methods of LINQ:

  • From
  • Where
  • Select

More about LINQ.

Demonstration

Step 1: Open Visual Studio, click Add, then new Web Site:

New WebSite

Add LINQ to SQL Classes:

LinqToSql Class

Step 2: Drag your SQL Table to dbml Layout:

dbml Layout

It will automatically create Class , Collection and Properties.

  • Class: DataContext, Departmeant
  • Collection: Departments
  • Proreties: DeparmentID,DeapartmentName ,CapLimits, Location

Step 3: Add a GridView Control to the form and bind accordingly:

Add a Grid view Control

Step 4: In Code Behind (.cs Page):

  • To Bind Grid View:

    Bind Grid View

  • For Insert and Delete:

    Insert and Delete

  • For Update:

    For Update

Output will look like the following:

output

Hopefully, this article will help you to work on GridView and LINQ.