How to Bind Data to a GridView using DataTable in Asp.Net C# and Vbt

Next →

One of the most popular controls in Asp.Net is the GridView control. It is a very flexible control and yet binding it with a database is quite simple. In this article I’ll show you how to use a Gridview control and populate data by binding it using a DataTable from code behind.

GridView CodeBehind Data Bound

The Markup

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>
Code Behind (C#)
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 DATABASE WITH THE GRIDVIEW.
                    GridView.DataSource = dt;
                    GridView.DataBind();
                }
                catch (Exception ex)
                {
                    //
                }
            }
        }
    }
}
Vb
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 DATABASE WITH THE GRIDVIEW.
                        GridView.DataSource = dt
                        GridView.DataBind()
                    Catch ex As Exception
                        '
                    End Try
                End Using
            End Using
        End If
    End Sub
End Class

Next →