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.
- CREATE TABLE [dbo].[ProductsList](
- [ProductId] [int] IDENTITY(1000,1) NOT NULL,
- [ProductName] [nvarchar](50) NULL,
- [ProductPrice] [nvarchar](50) NULL,
- [ProductDescription] [nvarchar](max) NULL,
- [ProductImage] [nvarchar](100) NULL,
- CONSTRAINT [PK_ProductsList] PRIMARY KEY CLUSTERED
- (
- [ProductId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
-
- GO
- CREATE PROCEDURE spGetProductList
- AS
- BEGIN
- SELECT*FROM [dbo].[ProductsList]
- END
Screenshot of database table
Step 2
Open Visual Studio 2015, click on New Project, and create an empty web application project.
Screenshot for creating new project 1
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
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
Step 3
Double click on the webconfig file and connect the database. Write the following line of code.
- <connectionStrings>
- <add name="DBCS" connectionString="data source=DESKTOP-M021QJH\SQLEXPRESS; database=DemoDB; integrated security=true;"/>
- </connectionStrings>
Step 4 - Add web form in your project
Right click on project, select Add, and choose Web Form.
Screenshot adding web form 1
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.
Step 5
Add script and styles of bootstrap in head section of web form.
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <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.
- <body>
- <form id="form1" runat="server">
- <div class="container py-4">
- <h5 class="text-center text-uppercase">GridView control in asp.net</h5>
- <asp:GridView CssClass="table table-bordered" HeaderStyle-CssClass="bg-primary text-white" AutoGenerateColumns="false" ID="GridView1" runat="server">
- <Columns>
- <asp:BoundField HeaderText="ID" DataField="ProductId" />
- <asp:ImageField HeaderText="Image" ControlStyle-Height="50" DataImageUrlField="ProductImage"></asp:ImageField>
- <asp:BoundField HeaderText="Name" DataField="ProductName" />
- <asp:BoundField HeaderText="Price" DataField="ProductPrice" />
- <asp:BoundField HeaderText="Description" DataField="ProductDescription" />
- </Columns>
- </asp:GridView>
- </div>
- </form>
- </body>
Complete web form code
- <!DOCTYPE html>
-
- <html>
- <head runat="server">
- <title>GridView</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div class="container py-4">
- <h5 class="text-center text-uppercase">GridView control in asp.net</h5>
- <asp:GridView CssClass="table table-bordered" HeaderStyle-CssClass="bg-primary text-white" AutoGenerateColumns="false" ID="GridView1" runat="server">
- <Columns>
- <asp:BoundField HeaderText="ID" DataField="ProductId" />
- <asp:ImageField HeaderText="Image" ControlStyle-Height="50" DataImageUrlField="ProductImage"></asp:ImageField>
- <asp:BoundField HeaderText="Name" DataField="ProductName" />
- <asp:BoundField HeaderText="Price" DataField="ProductPrice" />
- <asp:BoundField HeaderText="Description" DataField="ProductDescription" />
- </Columns>
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
Step 7
Right click and view web form code. Write the following code.
- using System;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
-
- namespace BindDataControl_Demo
- {
- public partial class GridView : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- BindGridView();
- }
- }
- private void BindGridView()
- {
- string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
- using (SqlConnection con = new SqlConnection(CS))
- {
- SqlCommand cmd = new SqlCommand("spGetProductList", con);
- cmd.CommandType = CommandType.StoredProcedure;
- con.Open();
- GridView1.DataSource = cmd.ExecuteReader();
- GridView1.DataBind();
- }
- }
- }
- }
Step 8
Create a folder with name Products add some products images
Step 9
Run the project by pressing ctrl+F5.
Screenshot
Conclusion
In this article, I have explained how to dynamically bind GridView control from database step by step. I hope it will help you.