Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

Bind Grid View In ASP Dot Net

Viewed:  1333 
Posted On:  22/03/2015 09:35:14 
How we can bind Grid View in ASP.NET C# ? 

Here I am showing how to bind Grid View In ASP.NET C#:

Below is my ASPX Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>Binding Grid View In ASP.NET</title>
    <form id="form1" runat="server">
        <asp:GridView ID="GVStudent" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="GVStudent_PageIndexChanging"
            CellPadding="4" ForeColor="#333333" GridLines="None" Width="90%">
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <asp:BoundField DataField="Student_ID" HeaderText="Student ID" HeaderStyle-Width="15%">
                    <HeaderStyle Width="15%" HorizontalAlign="Left"></HeaderStyle>
                <asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-Width="15%">
                    <HeaderStyle Width="15%" HorizontalAlign="Left"></HeaderStyle>

<asp:BoundField DataField="Email" HeaderText="Email" HeaderStyle-Width="15%">
                    <HeaderStyle Width="15%" HorizontalAlign="Left"></HeaderStyle>
                <asp:BoundField DataField="Class" HeaderText="Class" HeaderStyle-Width="15%">
                    <HeaderStyle Width="15%" HorizontalAlign="Left"></HeaderStyle>
                <asp:BoundField DataField="EnrollYear" HeaderText="EnrollYear" HeaderStyle-Width="40%">
                    <HeaderStyle Width="15%" HorizontalAlign="Left"></HeaderStyle>
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />            
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />

Now My ASPX.CS Code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
    SqlDataAdapter da;
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)
    private void BindGrid()
        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Server=.;database=TestDB;UID=sa; pwd=SQL;";
        SqlCommand cmd = new SqlCommand("SELECT * FROM STUDENT", con);
        da = new SqlDataAdapter(cmd);
        if (!object.Equals(dt, null))
            if (dt.Rows.Count > 0)
                GVStudent.DataSource = dt;
                GVStudent.DataSource = null;

protected void GVStudent_PageIndexChanging(object sender, GridViewPageEventArgs e)
        GVStudent.PageIndex = e.NewPageIndex;

Now Run Application:


Image 1.

By dotnet  On  06/04/2016 01:30:50
By dotnet  On  06/04/2016 01:27:33
Stock Management System Project with Source Code
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us