Posted on 2004-11-30
Last Modified: 2010-04-23
I have a growing problem. I have VS2003 Web Application VB.Net using datagrid for data coming from SQL. Everyone must have scrolling no one wants paging. Everyone must have sorting.

The problem is the datagrid is getting longer and so I need to make the header "frozen" so the data at the bottom of the grid will display the header row. I have done this before by hiding the datagrid header and create a new header above the grid but that won't work this time because of the sorting.

Thanks for any help/suggestions
Question by:Donnie4572
    LVL 28

    Accepted Solution

    Check this. It is scrollable datagrid with frozen header

    The idea is to make separate header and put it between div tags. Datagrid set headder to none

    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="ScrollDG.aspx.vb" Inherits="TestEverything.ScrollDG" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
                <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
                <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
                <meta content="JavaScript" name="vs_defaultClientScript">
                <meta content="" name="vs_targetSchema">
          <body MS_POSITIONING="GridLayout">
                <form id="Form1" method="post" runat="server">
                      <asp:Label id="Label1" runat="server" style="Z-INDEX: 103; LEFT: 112px; POSITION: absolute; TOP: 8px"
                            Font-Size="Large" Width="623px" Font-Bold="True" ForeColor="Red" BorderStyle="Solid">Example of scrollable datagrid with frozen header</asp:Label>
                      <asp:hyperlink id="Hyperlink2" style="Z-INDEX: 105; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server"
                            <table style="Z-INDEX: 101; LEFT: -9px; WIDTH: 707px; POSITION: relative; BORDER-COLLAPSE: collapse; HEIGHT: 26px"
                                  cellSpacing="0" align="left" border="1">
                                        <td align="center" width="195" bgColor="navy"><font face="Verdana" color="white" size="4"><b>YYYYMM
                                        <td align="center" width="195" bgColor="navy" style="WIDTH: 195px"><font face="Verdana" color="white" size="4"><b>Prod
                                        <td align="center" width="300" bgColor="navy"><font face="Verdana" color="white" size="4"><b>Name</b></font>
                      <div style="Z-INDEX: 102; LEFT: 2px; VERTICAL-ALIGN: top; OVERFLOW: auto; WIDTH: 710px; POSITION: absolute; TOP: 100px; HEIGHT: 300px">
                            <asp:datagrid id="dg" runat="server" ShowHeader="False" AutoGenerateColumns="False" BorderColor="Black"
                                        <asp:BoundColumn DataField="YYYYMM" HeaderText="YYYYMM">
                                              <ItemStyle HorizontalAlign="Center" Width="200px"></ItemStyle>
                                        <asp:BoundColumn DataField="ProdID" HeaderText="ProdID">
                                              <ItemStyle HorizontalAlign="Center" Width="200px"></ItemStyle>
                                        <asp:BoundColumn DataField="Name" HeaderText="Name">
                                              <ItemStyle HorizontalAlign="Center" Width="300px"></ItemStyle>

    Code behind

    Imports System.Data.OleDb

    Public Class ScrollDG
        Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub
        Protected WithEvents dg As System.Web.UI.WebControls.DataGrid
        Protected WithEvents Label1 As System.Web.UI.WebControls.Label
        Protected WithEvents Hyperlink2 As System.Web.UI.WebControls.HyperLink

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
        End Sub

    #End Region
        Dim ds As New DataSet
        Dim fsConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                 "Data Source=C:\TestDB\TestDB.mdb;"
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            dg.DataSource = ds
        End Sub
        Sub FillDataSet()
            Dim sSQL As String
            sSQL = "select YYYYMM, ProdID, Name from PRQ"

            Dim cn As New OleDbConnection(fsConn)
            ' Fill the dataset ds
            Dim da As New OleDbDataAdapter(sSQL, cn)
        End Sub

    End Class
    LVL 12

    Author Comment

    This is works for scrolling...frozen header. I don't see how to fit sorting into the code.

    Im using the sortcommand event to sort which fires from the column header.

    Private Sub mydatagrid_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles mydatagrid.SortCommand
            mydatagrid.DataSource = mydataset1.mytable.DefaultView
            mydataset1.mytable.DefaultView.Sort = e.SortExpression

        End Sub

    LVL 12

    Author Comment

    Any More Ideas?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now