ASP.Net GridView With LinqDataSource


In this article I provide a walkthrough of the use of an ASP.Net GridView with LinqDatasource.

You can bind a GridView with a LinqDataSource and be able to:

  1. Edit
  2. Delete
  3. View data without writing a single line of code.

To see how it works, follow the steps below.

Step 1

Create an ASP.Net Web Application.

ASPLINQ1.gif

Step 2

Right-click and add a new item to your ASP.Net Web Application project. Choose LINQ to SQL class from Data tab.

ASPLINQ2.gif

Drag and drop tables from the Server Explorer to generate a Data Context class. I am creating a Data Context class from a School Database by selecting the Person table.

ASPLINQ3.gif

Now the DataContext class has been created.

Step 3

Go ahead and drag and drop a GridView onto the Default.aspx page.

ASPLINQ4.gif

Switch to the design view on the Default.aspx page. Select properties of GridView by clicking on the arrow key at the right top of the GridView and then Choose Data Source option; then select "New data source".

ASPLINQ5.gif

From the dialog box you need to choose LINQ and if you want you can edit the data source name as well. I am leaving the default data source name LinqDataSource1.

ASPLINQ6.gif

Next you need to choose the context object . We have already created a DataContext named DataClasses1DataContext1. Choose DataClasses1DataContext1 as the context object.

ASPLINQ7.gif

After choosing a context object click the Next button. In the next dialog box choose the table and columns you want to bind the GridView to. Click on the advanced button:

ASPLINQ8.gif

Since you want data in the GridView to be editable, go ahead and check all three options.

ASPLINQ9.gif

Click Ok and click on Finish.

Step 4

Since you want to Edit, Delete and Insert data from the GridView, open the properties of the GridView and enable the options by checking the chekboxes.

ASPLINQ10.gif

You have done it . Press F5 to run the application. You will be able to Edit, Delete and View the data in the GridView without writing a single line of code.

ASPLINQ11.gif

I hope this article was useful. Thanks for reading.
 

Up Next
    Ebook Download
    View all
    Learn
    View all