How To Freez Header Of Gridview

<%@ Page Language="C#" AutoEventWireup="true"
  Inherits="MoveUpDOwnGridview" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="">
<head id="Head1" runat="server">

  <form id="form1" runat="server">
      <table style="border: 1px solid black; width: 600px" cellpadding="0" cellspacing="0">
              <td width="193px" nowrap style="border-right: 1px solid black">
              <td width="195px" nowrap style="border-right: 1px solid black">
              <td width="212px" nowrap>
              <td colspan="3" width="600px" style="border-top: 1px solid black">
                  <asp:Panel ID="Panel1" runat="server" Height="250px"
 ScrollBars="Vertical" Width="600px">
                      <asp:GridView ID="GridView1" ShowHeader="false" runat="server"
                              <asp:BoundField DataField="Id" HeaderText="Id"
ItemStyle-Width="200px" />
                              <asp:BoundField DataField="Address" HeaderText="Address"
ItemStyle-Width="200px" />
                              <asp:BoundField DataField="City" HeaderText="City"
ItemStyle-Width="200px" />

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MoveUpDOwnGridview : System.Web.UI.Page
   private int _i = 0;
   protected void Page_Load(object sender, EventArgs e)

   public DataTable GetCustomMadeDataTable()

       //Create a new DataTable object
       System.Data.DataTable objDataTable = new System.Data.DataTable();
       //Create three columns with string as their type
       objDataTable.Columns.Add("Id", typeof(int));
       objDataTable.Columns.Add("Address", typeof(string));
       objDataTable.Columns.Add("City", typeof(string));
       objDataTable.Columns.Add("Postalcode", typeof(string));
       //Adding some data in the rows of this DataTable
       DataRow dr;
       int intIndex = 900;
       for (int i = 1; i <= 101; i++)

           dr = objDataTable.NewRow();
           dr[0] = i;
           dr[1] = "Address" + i.ToString();
           dr[2] = "City" + i.ToString();
           dr[3] = "Postalcode" + intIndex.ToString();


       DataColumn[] dcPk = new DataColumn[1];
       dcPk[0] = objDataTable.Columns["Id"];
       objDataTable.PrimaryKey = dcPk;
       Session["strTemp"] = objDataTable;

       return objDataTable;

   public void BindGrid()
       if (Session["strTemp"] != null)

           GridView1.DataSource = Session["strTemp"] as DataTable;

           GridView1.DataSource = GetCustomMadeDataTable();

The GridView in this post don’t support sorting (But could easy be added if you rebind the data source to the Grid with server side code). You also need to specify the header manually; it will not get the header from the data source associated to the GridView. To add a scroller to the Panel, you can use the Panel control’s ScrollBars property.

No comments:

Post a Comment