Dynamically Bind GridView Control In ASP.NET From Database - Part Four

Introduction

Grid View displays the values of a data source in a table where each column represents a field and each row represents a record. The GridView control enables you to select, sort, and edit these items. In this article, I will demonstrate how to dynamically bind GridView control in asp.net from the database.

Step 1

Create database table in SQL Server 2014.

  1. CREATE TABLE [dbo].[ProductsList](    
  2.     [ProductId] [int] IDENTITY(1000,1) NOT NULL,    
  3.     [ProductName] [nvarchar](50) NULL,    
  4.     [ProductPrice] [nvarchar](50) NULL,    
  5.     [ProductDescription] [nvarchar](maxNULL,    
  6.     [ProductImage] [nvarchar](100) NULL,    
  7.  CONSTRAINT [PK_ProductsList] PRIMARY KEY CLUSTERED     
  8. (    
  9.     [ProductId] ASC    
  10. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]    
  11. ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]    
  12.     
  13. GO   
  1. CREATE PROCEDURE spGetProductList    
  2. AS    
  3. BEGIN    
  4. SELECT*FROM [dbo].[ProductsList]    
  5. END 
Screenshot of database table

ASP.NET 

Step 2

Open Visual Studio 2015, click on New Project, and create an empty web application project.

Screenshot for creating new project 1

 ASP.NET 

After clicking on New Project, one window will appear. Select Web from the left panel, choose ASP.NET Web Application, give a meaningful name to your project, then click on OK.

Screenshot for creating new project 2

 ASP.NET

After clicking on OK, one more window will appear. Choose Empty. check on Web Forms checkbox, and click on OK. 

Screenshot for creating new project 3

ASP.NET 

Step 3

Double click on the webconfig file and connect the database. Write the following line of code.

  1. <connectionStrings>  
  2.   <add name="DBCS" connectionString="data source=DESKTOP-M021QJH\SQLEXPRESS; database=DemoDB; integrated security=true;"/>  
  3. </connectionStrings>  

Step 4 - Add web form in your project

Right click on project, select Add, and choose Web Form.

Screenshot adding web form 1

ASP.NET 

Screenshot adding web form 2

After clicking on web form the window will appear; give a name to web form as DataList or any meaningful name. Click on OK. The web form will be added to the project.

ASP.NET 

Step 5

Add script and styles of bootstrap in head section of web form.

  1. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">  
  2.    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
  3.    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>  

Step 6

Drag and drop a GridView control on the web form. Design GridView control.

  1. <body>  
  2.     <form id="form1" runat="server">  
  3.         <div class="container py-4">  
  4.                  <h5 class="text-center text-uppercase">GridView control in asp.net</h5>  
  5.             <asp:GridView CssClass="table table-bordered" HeaderStyle-CssClass="bg-primary text-white" AutoGenerateColumns="false" ID="GridView1" runat="server">  
  6.                 <Columns>  
  7.                     <asp:BoundField HeaderText="ID" DataField="ProductId" />  
  8.                     <asp:ImageField HeaderText="Image" ControlStyle-Height="50" DataImageUrlField="ProductImage"></asp:ImageField>  
  9.                     <asp:BoundField HeaderText="Name" DataField="ProductName" />  
  10.                     <asp:BoundField HeaderText="Price" DataField="ProductPrice" />  
  11.                     <asp:BoundField HeaderText="Description" DataField="ProductDescription" />  
  12.                 </Columns>  
  13.             </asp:GridView>  
  14.         </div>  
  15.     </form>  
  16. </body>  

Complete web form code

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4. <head runat="server">  
  5.     <title>GridView</title>  
  6.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">  
  7.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
  8.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>  
  9. </head>  
  10. <body>  
  11.     <form id="form1" runat="server">  
  12.         <div class="container py-4">  
  13.                  <h5 class="text-center text-uppercase">GridView control in asp.net</h5>  
  14.             <asp:GridView CssClass="table table-bordered" HeaderStyle-CssClass="bg-primary text-white" AutoGenerateColumns="false" ID="GridView1" runat="server">  
  15.                 <Columns>  
  16.                     <asp:BoundField HeaderText="ID" DataField="ProductId" />  
  17.                     <asp:ImageField HeaderText="Image" ControlStyle-Height="50" DataImageUrlField="ProductImage"></asp:ImageField>  
  18.                     <asp:BoundField HeaderText="Name" DataField="ProductName" />  
  19.                     <asp:BoundField HeaderText="Price" DataField="ProductPrice" />  
  20.                     <asp:BoundField HeaderText="Description" DataField="ProductDescription" />  
  21.                 </Columns>  
  22.             </asp:GridView>  
  23.         </div>  
  24.     </form>  
  25. </body>  
  26. </html>  

Step 7

Right click and view web form code. Write the following code.

  1. using System;  
  2. using System.Configuration;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5.   
  6. namespace BindDataControl_Demo  
  7. {  
  8.     public partial class GridView : System.Web.UI.Page  
  9.     {  
  10.         protected void Page_Load(object sender, EventArgs e)  
  11.         {  
  12.             if (!IsPostBack)  
  13.             {  
  14.                 BindGridView();  
  15.             }  
  16.         }  
  17.         private void BindGridView()  
  18.         {  
  19.             string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;  
  20.             using (SqlConnection con = new SqlConnection(CS))  
  21.             {  
  22.                 SqlCommand cmd = new SqlCommand("spGetProductList", con);  
  23.                 cmd.CommandType = CommandType.StoredProcedure;  
  24.                 con.Open();  
  25.                 GridView1.DataSource = cmd.ExecuteReader();  
  26.                 GridView1.DataBind();  
  27.             }  
  28.         }  
  29.     }  
  30. }  

Step 8

Create a folder with name Products add some products images 

Step 9

Run the project by pressing ctrl+F5.

Screenshot

ASP.NET

 

Conclusion

In this article, I have explained how to dynamically bind GridView control from database step by step. I hope it will help you.


Similar Articles