How to bind data to a gridView control in using datatable with c#

In this article, I am going to explain with an example how to create a bind data to a gridView control in using data table with c#.

One of the most popular controls in Asp.Net is GridView control.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridview.aspx.cs" Inherits="gridview" %>

    <!DOCTYPE html>
        <title>GridView Control Data Bind In Data Table</title>
        <form id="form1" runat="server">
                <asp:GridView ID="GridView" Font-Names="Arial" Font-Size="0.75em" runat="server">


Source Code C#

  1. 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)
            string constr = "Connection String";

            using (SqlConnection con = new SqlConnection(constr))
                using (SqlCommand cmd = new SqlCommand("SELECT *FROM Table_Name"))
                    SqlDataAdapter sda = new SqlDataAdapter();
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;
                        DataTable dt = new DataTable();
                        GridView.DataSource = dt;
                    catch (Exception ex)



Post a Comment