Last Updated: 18th June 2025
The GridView control in ASP.NET is one of the most widely used controls for displaying data in a tabular format. It is a powerful and flexible control, allowing developers to manage data with minimal effort. One of the key advantages of the GridView is its ease of integration with a database.In this tutorial, we will walk you through the process of binding data to the GridView control using a DataTable from the code-behind in ASP.NET. Whether you're new to ASP.NET or looking to enhance your skills, this guide will provide step-by-step instructions to help you populate your GridView with dynamic data.
All we need is a GridView control that we will bind with an SQL Server table, from code behind.
<!DOCTYPE html> <html> <head> <title>GridView Code Behind DataBound</title> </head> <body> <form id="frmGridViewDemo" runat="server"> <div> <asp:GridView ID="GridView" Font-Names="Arial" Font-Size="0.75em" CellPadding="5" CellSpacing="0" ForeColor="#333" runat="server"> <HeaderStyle BackColor="#989898" ForeColor="white" /> </asp:GridView> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // SET THE CONNECTION STRING. string sCon = "Data Source=dna;Persist Security Info=False;" + "Integrated Security=SSPI;" + "Initial Catalog=DNA_CLASSIFIED;User Id=sa;Password=demo;Connect Timeout=30;"; using (SqlConnection con = new SqlConnection(sCon)) { using (SqlCommand cmd = new SqlCommand("SELECT *FROM dbo.Employee")) { SqlDataAdapter sda = new SqlDataAdapter(); try { cmd.Connection = con; con.Open(); sda.SelectCommand = cmd; DataTable dt = new DataTable(); sda.Fill(dt); ' Bind the database to the GridView control. GridView.DataSource = dt; GridView.DataBind(); } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); } } } } }
Remember: The using statement ensures that objects like the SqlConnection, SqlCommand, and SqlDataAdapter are automatically disposed of when no longer needed, preventing memory leaks or connection issues.
Error Handling: Provides a better foundation for logging exceptions, which can help during debugging or when providing feedback to users.
Option Explicit On Imports System.Data ' FOR "DataTable" Imports System.Data.SqlClient Partial Class _Default Inherits System.Web.UI.Page Protected Sub frmGridViewDemo_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles frmGridViewDemo.Load If Not IsPostBack Then ' Set the connection string. Dim sCon As String = "Data Source=intel;Persist Security Info=False;" & _ Integrated Security=SSPI;" & _ "Initial Catalog=DNA_CLASSIFIED;User Id=sa;Password=demo;Connect Timeout=30;" Using con As SqlConnection = New SqlConnection(sCon) Using cmd As SqlCommand = New SqlCommand("SELECT *FROM dbo.Employee") Dim sda As SqlDataAdapter = New SqlDataAdapter Try cmd.Connection = con : con.Open() sda.SelectCommand = cmd Dim dt As DataTable = New DataTable sda.Fill(dt) ' Bind the database to the GridView control. GridView.DataSource = dt GridView.DataBind() Catch ex As Exception Console.WriteLine("An error occurred: " & ex.Message) Finally ' Ensure the connection is closed in case of an exception. If con.State = ConnectionState.Open Then con.Close() End If End Try End Using End Using End If End Sub End Class
Conclusion
Binding data to the GridView control in ASP.NET using a DataTable is an efficient way to display dynamic data in a structured, tabular format. By following the steps outlined in this tutorial, you can easily connect your GridView to a DataTable and populate it with data from various sources, such as databases or in-memory data.
With the GridView's built-in capabilities for sorting, paging, and editing, it remains one of the most powerful and flexible controls in ASP.NET. By mastering how to bind data properly, you can create highly interactive and responsive web applications with ease.