ASP.Net GridView with LinqDataSource

In this post I will walkthrough on using ASP.Net Gridview with LinqDatasource.

You can bind a GridView with LinqDataSource and would 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;

Step1

Crate a ASP.Net Web Application.

clip_image002

Step 2

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

clip_image004

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

clip_image005

Now Data Context class has been created.

Step 3

Go ahead and drag and drom a GridView on Default.aspx page.

clip_image006

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

clip_image007

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 default data source name LinqDataSource1

clip_image009

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

clip_image011

After choosing context object click on Next button . From next dialog box choose table and columns you want to bind grid view . Click on the advanced button,

clip_image013

Since you want data in grid view editable. So go ahead and checked all the three options.

clip_image014

Click Ok and clcik on Finish

Step 4

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

clip_image015

 

You have done it . Press F5 to run the appliction. You would able to Edit, Delete and View the data in Grid View without writing a single line of code.

clip_image016

I hope this post was useful. Thanks for reading Smile

 

***********************

 

Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Yahoo BuzzAdd to Newsvine

7 responses to “ASP.Net GridView with LinqDataSource”

  1. nice,simple one
    Thanks

  2. thanks. really simple and working solution

  3. Thank you very much. This has become my best material on LINQ topic. You made it very simple and understandable, thanks for sharing with us. Some other good articles on LINQ I was found over internet during searching this topic which also explained very well about LINQ, URL links of those posts are….
    http://msdn.microsoft.com/en-us/library/bb907622.aspx
    http://mindstick.com/Articles/9ca8fabd-49ef-4e4d-855b-74fc523d9138/?LINQ%20%28Language%20Integrated%20Query%29
    http://www.codeproject.com/Articles/188935/LINQ-Demo-with-ASP-NET-Web-Application

    Lastly, I would like to say thanks to everyone for your precious post.

  4. Not working for edit and delete LinqDataSource ‘LinqDataSource1’ does not support the Select property when the Delete, Insert or Update operations are enabled.

    PLEASE HELP ME ?

  5. Can you give me one example on the same thing
    display two tables data in a single Gridview And also perform Update delete operations,
    any methos plz help me the complete simple example

Leave a comment

Create a website or blog at WordPress.com