Net-informations.com
SiteMap  | About    

ASP.NET Simple GridView

The GridView control provides many built-in capabilities that allow the user to sort, update, delete, select, and page through items in the control. The GridView control can be bound to a data source control, in order to bind a data source control, set the DataSourceID property of the GridView control to the ID value of the data source control.

asp.net gridview

Download Database

In this article I have used Microsoft's Pubs database for sample data. You can download it free from the following link.

Download

Before you start to generate GridView in your asp file, you should create a ConnectionString in your web.Config File. Double click the web.config file on the right hand side of the Visual Studio and add the following connectionstring code in that file.

web.config file

Web.Config File

  

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
	<add name="SQLDbConnection"
	 connectionString="Server=Your-Server-Name; Database=pubs; User Id=sa; password= your-passoword"
	 providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

After create the connectionstring in the web.config file, you should call the connectionstring in your asp page and issue the select command.

  

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SQLDbConnection %>"
SelectCommand="select au_lname,phone,address,city from [authors]" />

When you run this asp.net application , your output look likes the following image

simple-gridview

The following is the full asp source code for selecting au_lname,phone,address,city from the authors table in the Pubs database.

Asp.net gridview example

Default.aspx

  

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
	<div>
		<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" />
		<asp:SqlDataSource ID="SqlDataSource1" runat="server"
		ConnectionString="<%$ ConnectionStrings:SQLDbConnection %>"
		SelectCommand="select au_lname,phone,address,city from [authors]" />
	</div>
    </form>
</body>
</html>







net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.