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

Bind Grid View In ASP Dot Net

Article:
Viewed:  901 
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Binding Grid View In ASP.NET</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GVStudent" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="GVStudent_PageIndexChanging"
            CellPadding="4" ForeColor="#333333" GridLines="None" Width="90%">
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <Columns>
                <asp:BoundField DataField="Student_ID" HeaderText="Student ID" HeaderStyle-Width="15%">
                    <HeaderStyle Width="15%" HorizontalAlign="Left"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-Width="15%">
                    <HeaderStyle Width="15%" HorizontalAlign="Left"></HeaderStyle>
                </asp:BoundField>

                
<asp:BoundField DataField="Email" HeaderText="Email" HeaderStyle-Width="15%">
                    <HeaderStyle Width="15%" HorizontalAlign="Left"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField DataField="Class" HeaderText="Class" HeaderStyle-Width="15%">
                    <HeaderStyle Width="15%" HorizontalAlign="Left"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField DataField="EnrollYear" HeaderText="EnrollYear" HeaderStyle-Width="40%">
                    <HeaderStyle Width="15%" HorizontalAlign="Left"></HeaderStyle>
                </asp:BoundField>
            </Columns>
            <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" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>
 

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)
            BindGrid();
    }
 
    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);
        da.Fill(dt);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
 
        if (!object.Equals(dt, null))
        {
            if (dt.Rows.Count > 0)
            {
                GVStudent.DataSource = dt;
                GVStudent.DataBind();
            }
            else
            {
                GVStudent.DataSource = null;
                GVStudent.DataBind();
            }
        }
    }

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

Now Run Application:

1.png

Image 1.

  Comment:
 
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 http://allittechnologies.blogspot.in/2015/05/stock-management-system-project-for-asp.net-beginners-experience-in-software-it-technologies.html
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us