Home > Uncategorized > GridView with Fixed Header and Scroll Bar

GridView with Fixed Header and Scroll Bar

Hi all,

try this example to create GridView with Fixed Header and Scroll Bar

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Fixed Header GridView</title>
<style type="text/css">
.FixedHeader {
position: absolute;
font-weight: bold;
<form id="form1" runat="server">
<h2 align="center">GridView with Fixed Header</h2>
<div style="height: 400px; overflow: auto" align="center">
<asp:GridView ID="gvDistricts" runat="server" HeaderStyle-CssClass="FixedHeader" HeaderStyle-BackColor="YellowGreen"
AutoGenerateColumns="false" AlternatingRowStyle-BackColor="WhiteSmoke" OnRowDataBound="gvDistricts_RowDataBound">
<asp:TemplateField HeaderText="District ID" HeaderStyle-Width="80px" ItemStyle-Width="80px">
<asp:Label ID="lblDistID" runat="server" Text='<%#Eval("DistID")%>'></asp:Label>
<asp:TemplateField HeaderText="District Name" HeaderStyle-Width="120px" ItemStyle-Width="120px">
<asp:Label ID="lblDistName" runat="server" Text='<%#Eval("DistName")%>'></asp:Label>
<asp:TemplateField HeaderText="Description" HeaderStyle-Width="200px" ItemStyle-Width="200px">
<asp:Label ID="lblDistDesc" runat="server" Text='<%#Eval("DistDesc")%>'></asp:Label>

In Code behind :

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 = "Data Source=.\\sqlexpress;Initial Catalog=MYDB;Integrated Security=True";
if (!IsPostBack)
string Query = "SELECT * FROM Districts";
SqlConnection con = new SqlConnection(ConStr);
SqlDataAdapter adp = new SqlDataAdapter(Query, con);
DataTable dt = new DataTable();
gvDistricts.DataSource = dt.DefaultView;

protected void gvDistricts_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
if (e.Row.RowIndex == 0)
e.Row.Style.Add("height", "50px");

Hope this helps

Good Luck

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: