We can direct select,update and delete the values of the database by Grid View using Linq Data Source.
Introduction
Linq Data Source is used to directly connect to the Sql Data Source.
The LinqDataSource control exposes Language-Integrated Query (LINQ) to Web developers through the ASP.NET data-source control architecture
Objective
At first add Linq To Sql Class, by right clicking on the solution Explorer. After adding that you have to add DataClasses.dbml in that add the table from which you want to link the grid view bu just drag and drop the table to the .dbml from the solution explorer.. After that add a LinqDataSource to the .aspx file on clicking on that add data souce that will automatically come on that. After that add the grid view and choose the datasource. Now you can select, insert and delete directly to the data source.
Using the code
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><
html xmlns="http://www.w3.org/1999/xhtml"><
head id="Head1" runat="server"><title>Untitled Page</title></
head><
body>
<form id="form1" runat="server"><div></div><asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="DataClassesDataContext" EnableDelete="True" EnableInsert="True" EnableUpdate="True" TableName="Bus_Details"></asp:LinqDataSource><asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="busno" DataSourceID="LinqDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" /><asp:BoundField DataField="travels" HeaderText="travels" SortExpression="travels" /><asp:BoundField DataField="busno" HeaderText="busno" ReadOnly="True" SortExpression="busno" /><asp:BoundField DataField="source" HeaderText="source" SortExpression="source" /><asp:BoundField DataField="destination" HeaderText="destination" SortExpression="destination" /><asp:BoundField DataField="date" HeaderText="date" SortExpression="date" /><asp:BoundField DataField="time" HeaderText="time" SortExpression="time" /><asp:BoundField DataField="category" HeaderText="category" SortExpression="category" /><asp:BoundField DataField="amount" HeaderText="amount" SortExpression="amount" /><asp:BoundField DataField="kilometers" HeaderText="kilometers" SortExpression="kilometers" /><asp:BoundField DataField="Mo" HeaderText="Mo" SortExpression="Mo" /><asp:BoundField DataField="Tu" HeaderText="Tu" SortExpression="Tu" /><asp:BoundField DataField="We" HeaderText="We" SortExpression="We" /><asp:BoundField DataField="Th" HeaderText="Th" SortExpression="Th" /><asp:BoundField DataField="Fr" HeaderText="Fr" SortExpression="Fr" /><asp:BoundField DataField="Sa" HeaderText="Sa" SortExpression="Sa" /><asp:BoundField DataField="Su" HeaderText="Su" SortExpression="Su" /></Columns></asp:GridView></form></
body></
html>
Below is the way to configure the LinqDataSource control.
Like this you can Configure the Linq Data Source. This will open a windows form like this.
After that By clicking on Next you will get...
And by Clicking to the Finish Button the Building of the linq data Source is Finished.
After that add LinqDataSource to the GridView.
Now by Enabling Paging, Selecting,Editing and Deleting you can update,select and delete directly to the database using GridView
Conclusion
You can do many more thing using Linq. Its very useful for connecting to the database.