Tips and tricks for .NET using ASP and VB code.

GridView with scrollable data and fixed header

    Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Dim ThisGridView As GridView = sender
        If TypeOf e.Row.DataItem Is Data.DataRowView Then
            Dim GridDataRowView As Data.DataRowView = e.Row.DataItem
            Dim GridDataView = GridDataRowView.DataView
            If e.Row.DataItemIndex = GridDataView.Count - 1 Then
                'At the end of the data, add a row containing the header texts so the columns will be spaced correctly.
                Dim NewGridViewRow As New GridViewRow(0, Nothing, DataControlRowType.DataRow, DataControlRowState.Normal)
                NewGridViewRow.Style.Item("visibility") = "hidden"
                For Each HeaderDataControlFieldCell As DataControlFieldCell In ThisGridView.HeaderRow.Cells
                    Dim NewGridViewCell As New TableCell
                    NewGridViewCell.Text = HeaderDataControlFieldCell.ContainingField.HeaderText
                    NewGridViewCell.Font.Bold = True
                    NewGridViewRow.Cells.Add(NewGridViewCell)
                    Dim HeaderCellId As String = HeaderDataControlFieldCell.ClientID
                    Dim NewCellId As String = ThisGridView.ClientID & "_" & NewGridViewCell.ClientID
                    Dim SendJavaScriptToBrowser As ClientScriptManager = Page.ClientScript
                    'Using JavaScript, make the header columns the same width as those in the newly added row.
                    Dim JavaScriptText As String = String.Format("document.getElementById('{0}').style.width = document.getElementById('{1}').clientWidth + 'px'; ", HeaderCellId, NewCellId)
                    SendJavaScriptToBrowser.RegisterStartupScript(SendJavaScriptToBrowser.GetType, Now.ToBinary.ToString, JavaScriptText, True)
                    Threading.Thread.Sleep(18)
                Next HeaderDataControlFieldCell
                ThisGridView.Controls(0).Controls.Add(NewGridViewRow)
            End If
        End If
    End Sub

    Protected Sub GridView1_PreRender(sender As Object, e As System.EventArgs) Handles GridView1.PreRender
        Dim ThisGridView As GridView = sender
        ThisGridView.CssClass = "ScrollingGridView"
        ThisGridView.BorderWidth = 0
        ThisGridView.UseAccessibleHeader = True
        ThisGridView.HeaderRow.TableSection = TableRowSection.TableHeader
        ThisGridView.ShowFooter = False
        ThisGridView.ShowHeader = True
    End Sub

Required CSS

        .ScrollingGridView *
        {
            margin: 0;
            padding: 0;
            border-spacing: 10;
            border: 1;
        }
        tbody
        {
            display: block;
            position: relative;
            width: 97%;
            height: 50%;
            overflow: auto;
        }
        
        thead
        {
            position: relative;
            display: block;
        }
        tr
        {
            height: auto;
        }

No comments:

Post a Comment

Search This Blog