Monday, 23 January 2012

ASP.Net GridView with LinqDataSource,data bindingin ASP.Net GridView with LinqDataSource


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


Posted by Smile

No comments :