Grid View With Fixed Header In Asp.net

Wednesday, 12 March 2014

Grid View With Fixed Header In Asp.net

Grid View With Fixed Header In Asp.net

Requirement  :
  • A Grid view with auto generated column and Row ()
  • A Div Above the grid 
Functionality :
In this case we are hiding the header of the grid view and coping each column with style in the div


Aspx Page Style Codding

  <style type="text/css">
        body
        {
            background-color: #333;
        }
       
        .GridViewStyle
        {
            font-family: Verdana;
            font-size: 11px;
            background-color: White;
        }
       
        .GridViewHeaderStyle
        {
             height: 15px;
            font-family: Verdana;
            font-size: 12px;
            color: black;
            background: black url(Image/SubHeader.png) repeat-x;
        }
       
         .GridViewRowStyle
        {
            font-family: Verdana;
            font-size: 11px;           
        }
    </style>

Scripting

<script  type="text/javascript">
       function CreateGridHeader(DataDiv, GridView_draft_details, HeaderDiv) {
           var DataDivObj = document.getElementById(DataDiv);
           var DataGridObj = document.getElementById('<%= GridView_draft_details.ClientID %>');
            var HeaderDivObj = document.getElementById(HeaderDiv);

            //********* Creating new table which contains the header row ***********
            var HeadertableObj = HeaderDivObj.appendChild(document.createElement('table'));

            DataDivObj.style.paddingTop = '0px';
            var DataDivWidth = DataDivObj.clientWidth;
            DataDivObj.style.width = '110%';
            // DataDivObj.border = '1px'
            DataDivObj.style.border = "1px solid Black";
            //********** Setting the style of Header Div as per the Data Div ************
            HeaderDivObj.className = DataDivObj.className;
            HeaderDivObj.style.cssText = DataDivObj.style.cssText;
            //**** Making the Header Div scrollable. *****
            HeaderDivObj.style.overflow = 'auto';
            //*** Hiding the horizontal scroll bar of Header Div ****
            HeaderDivObj.style.overflowX = 'hidden';
            //**** Hiding the vertical scroll bar of Header Div ****
            HeaderDivObj.style.overflowY = 'hidden';
            HeaderDivObj.style.height = DataGridObj.rows[0].clientHeight + 'px';
            //**** Removing any border between Header Div and Data Div ****
            HeaderDivObj.style.borderBottomWidth = '0px';

            //********** Setting the style of Header Table as per the GridView ************
            HeadertableObj.className = DataGridObj.className;
            //**** Setting the Headertable css text as per the GridView css text
            HeadertableObj.style.cssText = DataGridObj.style.cssText;
            HeadertableObj.border = '1px';
            HeadertableObj.rules = 'all';
            HeadertableObj.cellPadding = DataGridObj.cellPadding;
            HeadertableObj.cellSpacing = DataGridObj.cellSpacing;

            //********** Creating the new header row **********
            var Row = HeadertableObj.insertRow(0);
            Row.className = DataGridObj.rows[0].className;
            Row.style.cssText = DataGridObj.rows[0].style.cssText;
            Row.style.fontWeight = 'bold';

            //******** This loop will create each header cell *********
            for (var iCntr = 0; iCntr < DataGridObj.rows[0].cells.length; iCntr++) {
                var spanTag = Row.appendChild(document.createElement('td'));
                spanTag.innerHTML = DataGridObj.rows[0].cells[iCntr].innerHTML;
                var width = 0;
                //****** Setting the width of Header Cell **********
                if (spanTag.clientWidth > DataGridObj.rows[1].cells[iCntr].clientWidth) {
                    width = spanTag.clientWidth;
                }
                else {
                    width = DataGridObj.rows[1].cells[iCntr].clientWidth;
                }
                if (iCntr <= DataGridObj.rows[0].cells.length - 2) {
                    spanTag.style.width = width + 'px';
                }
                else {
                    spanTag.style.width = width + 20 + 'px';
                }
                DataGridObj.rows[1].cells[iCntr].style.width = width + 'px';
            }
            var tableWidth = DataGridObj.clientWidth;
            //********* Hidding the original header of GridView *******
            DataGridObj.rows[0].style.display = 'none';
            //********* Setting the same width of all the componets **********
            HeaderDivObj.style.width = DataDivWidth + 'px';
            DataDivObj.style.width = DataDivWidth + 'px';
            DataGridObj.style.width = tableWidth + 'px';
            HeadertableObj.style.width = tableWidth + 20 + 'px';
            return false;
        }

        function Onscrollfnction() {
            var div = document.getElementById('DataDiv');
            var div2 = document.getElementById('HeaderDiv');
            //****** Scrolling HeaderDiv along with DataDiv ******
            div2.scrollLeft = div.scrollLeft;
            return false;
        }

    </script>
AspxPage coding

<div align="center">
                    <%--Div contains the new header of the GridView--%>
                    <div id="HeaderDiv" style="width: 20px">
                    </div>
                    <%--Wrapper Div which will scroll the GridView--%>
                    <div id="DataDiv" style="overflow: auto; width: 94%; height: 450px;"
                        onscroll="Onscrollfnction();"><%--border: 1px solid olive; --%>
                        <asp:GridView ID="GridView1" runat="server"
                            AutoGenerateColumns="true" CssClass="GridViewStyle" CellSpacing="0" Height="100%"  >
                            <HeaderStyle CssClass="GridViewHeaderStyle" Height="30px"  Width="100%" />
                           <RowStyle CssClass="GridViewRowStyle" Width="100%" HorizontalAlign="Right" />
                        </asp:GridView>
                    </div>

                                      </div>


Coding For CodeBehind page
on button click event fill the grid view 

  dim dt = as datatable ()
dt = "Fill the datatable "
                GridView1.DataSource = dt
                GridView1.DataBind()