How to bind data to GridView control in ASP.NET using C# with example

3 Comments | 3821 Views | Post by: Admin | Tags: gridview in asp.net c#,gridview android,gridview example,gridview c#,gridview events,gridview tutorial,gridview wpf gridview paging


In this tutorial, we are going to learn How to bind data to Asp.Net GridView control in ASP.NET using C# with example.


In this tutorial, we are going to learn How to bind data to Asp.Net GridView control in ASP.NET using C# with example.

Let me explain you what is this Gridview, a Gridview control display 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.

So let’s Starts the Coding!!!

Note: for this article, I have used Northwind database. You can download the Northwind database from below Location

http://northwinddatabase.codeplex.com/releases/view/71634

After downloading the Northwind Database, attach the same in SQL Management Studio.

Step 1: Create a ASP.NET empty Web Application

Step 2: Create a webform “Default.aspx” and write the following codes.

Step 3: Drag and Drop GridView Control from Visual Studio Toolbox.

Step 4: Default.aspx codes will be like following codes.

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

"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
"http://www.w3.org/1999/xhtml">
"Head1" runat="server">
    


    "form1" runat="server">
    
"gvProduct" runat="server" Width="600px" AutoGenerateColumns="false"> "#3E3E3E" Font-Bold="True" Font-Names="cambria" ForeColor="White" Height="25px" /> "Calibri" Height="25px" /> "ProductID" HeaderText="Product ID" /> "ProductName" HeaderText="Product Name" /> "QuantityPerUnit" HeaderText="Quantity Per Unit" /> "UnitPrice" HeaderText="Unit Price" /> "UnitsInStock" HeaderText="Units In Stock" /> "UnitsOnOrder" HeaderText="Units On Order" />

Step 5: Code Behind will be the following.

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
{
    SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindList();
        }

    }

    private void BindList()
    {
        DataSet ds = new DataSet();
        string cmdstr = "select ProductID, ProductName, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder from Products";
        SqlDataAdapter adp = new SqlDataAdapter(cmdstr, conn);
        adp.Fill(ds);
        gvProduct.DataSource = ds;
        gvProduct.DataBind();
    }

}

Step 7: Run the application and now you will see the data in tabular form

Note: for demo purpose you can download the complete running solution at the end of this article.


Conclusion:-
So In this tutorial, We are going to learn How to bind data to GridView control in ASP.NET using C# with example. Happy Coding!!!!

 

Download Source Codes


Post Comments

if you have some suggestion, question, request on How to bind data to GridView control in ASP.NET using C# with example.

Please use the comment box to express your views.

 

Comments:

loading...