GridView Control in ASP.Net

Description: a GridView is a databound control for displaying and manipulating data in a web application. A GridView displays data in tabular format, in ohter words a collection of rows and columns. Here each row represents one record, each column represents one field in the database table.

Here I would like to bind the data to Griview using SqlDatasource.

SqlDataSource

  • SqlDataSource is a Web server control, it allows you to access the data from any relational database. It can be accessed from Microsoft SQL Server and Oracle databases. As well as we have OLEDB and ODBCDataSources also. 
  • You can use this SqlDataSource with any databound control like GridView, formview, treeview and so on to display and manipulate the data in webpages with no or little code. 
  • In SqlDatasource you need to specify SQL queries or a Stored Procedure name to execute, then SqlDataSource internally uses respective ADO.NET classes to perform operations on the database. 
Example to demonstrate the GridView with SQlDataSource:
 
Consider the following table Employee:



Set the ConnectionString in web.config.
  1. <connectionStrings>  
  2.    <add name="myconnection" connectionString="Data  Source=ABHI-PC\SQLEXPRESS;Initial Catalog=Articles;Integrated Security=True"  
  3.      providerName="System.Data.SqlClient" />  
  4.  </connectionStrings>  
Deault.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.         <asp:GridView ID="gv1" runat="server" AutoGenerateColumns="false"  
  13.         DataKeyNames="EmpId" DataSourceID="SqlDataSource1"  
  14.         HeaderStyle-BackColor="Green" AlternatingRowStyle-BackColor="LightPink"   
  15.         AutoGenerateEditButton="true"  AutoGenerateDeleteButton="true"          >  
  16.             <Columns>  
  17.                 <asp:BoundField HeaderText ="Employee Id" DataField="EmpId" />  
  18.                 <asp:BoundField HeaderText="Employee Name" DataField="EmpName" />  
  19.                 <asp:BoundField HeaderText="Employee EmailId" DataField="EmpEmailId" />  
  20.                 <asp:BoundField HeaderText="Mobile Number" DataField="EmpMobileNum" />  
  21.   
  22.             </Columns>  
  23.         </asp:GridView>  
  24.         <asp:SqlDataSource ID="SqlDataSource1" runat="server"  
  25.         ConnectionString="<%$ ConnectionStrings:myconnection %>"  
  26.         selectCommand="select * from employee"  
  27.         UpdateCommand="update employee set EmpName=@EmpName,EmpEmailId=@EmpEmailId,EmpMobileNum=@EmpMobileNum where EmpId=@EmpId"  
  28.         DeleteCommand="delete from employee where EmpId=@EmpId">  
  29.        </asp:SqlDataSource>  
  30.       
  31.     </div>  
  32.     </form>  
  33. </body>  
  34. </html>  


Similar Articles