Fix Gridview Header While Scrolling in Asp.net


gridview header fixed on scroll in asp.net,

In this article we discus about Gridview Header Fix while scrolling data, whenever we want to display large amount of data in Gridview and that they is small space is available to display gridview data in web pages. This kind of suitable can handle by using Gridview header fix with scrollable. let me show you how to achieve this and also you can see in above image

Database

In this articles i have used my previous project database Create Book Inventory Project.

To Fix Gridview Header :

Gridview Properties :

  • GridView1 ShowHeader = "false"

now create gridview header by using HTML Table and places the GridView just below the Table. So the header row is always fixed there and we can scroll the GridView and see the data.

HTML Markup : Create HTML Design


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

<!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></title>
    <style type="text/css">
     .grd_header_table
     {
         background:#ffffff;
         color:#fafafa;
         font-weight:bold;
         border:1px solid #FF0040;
         width:580px;
     }
    </style>
</head>
<body>
    <form id="form1" runat="server">
     <div class="grd_header_table">
        <table bgcolor="#FF0040" rules="all" >
            <tr>
                <td style ="width:55px;">BookID</td>
                <td style ="width:260px;">Book Name</td>
                <td style ="width:114px;">Author</td>
                <td style ="width:206px;">Pub. Name</td>
                <td style ="width:100px;">Pub. Date</td>
            </tr>
        </table>
        </div>
        <div style ="height:130px; width:600px; overflow:auto;">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            ShowHeader = "False" Width ="580px" >
                <Columns>
                    <asp:BoundField DataField="Book_Id" />
                    <asp:BoundField DataField="Book_name" />
                    <asp:BoundField DataField="Book_author" />
                    <asp:BoundField DataField="Book_Publisher_name" />
                    <asp:BoundField DataField="Book_Published_date" />
                </Columns>
            </asp:GridView>
            </div>
    </form>
</body>
</html>

C# Coding



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;
using System.Configuration;

public partial class GridviewFixedHeader : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbCon"].ToString());
    string query;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillGridData();
        }
    }
    private void FillGridData()
    {
        query = "select * from Book";
        SqlDataAdapter da = new SqlDataAdapter(query, con);
        DataSet ds = new DataSet();
        da.Fill(ds, "book");
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}

0 Komentar untuk "Fix Gridview Header While Scrolling in Asp.net"

Back To Top