[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 694
  • Last Modified:

freezing my datagrid headers

I have created a datagrid using visual studio and I added scrolling properties to it. It works great but the table headings move up when I scroll down the table. I am wondering if there is a way in .net via html, vbscript, or javascript to lock or freeze my headers so that the data will be the only thing scrolling up and down and not my headers? If there is please show me the code and how and where to add it in my visual studio project. I saw this question answered on this web site by creating a table above the datadrid and they could not get it to line up right so I am not going to do that. There has to be a way to do this I am sure I am not the first or the last person to run into this problem.
0
lvandevender
Asked:
lvandevender
  • 4
1 Solution
 
iboutchkineCommented:
If you are using a popup window you could transfer the data that filled the datagrid or the datagrid itself. You can use Session object for this:

2 forms: TransferControl and TransferControlPopup

TransferControl.aspx (add DataSet1) pull from Data tab
--------------------------------------------------------------------------------------------------
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="TransferControl.aspx.vb" Inherits="TestEverything.TransferControl"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>WebForm1</title>
            <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
            <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:hyperlink id="Hyperlink2" style="Z-INDEX: 105; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server"
                        NavigateUrl="PageManipulation.aspx">Page Manipulation</asp:hyperlink>
                  <asp:Button id="Button1" style="Z-INDEX: 107; LEFT: 477px; POSITION: absolute; TOP: 126px" runat="server"
                        Text="Transfer datagrid to popup window"></asp:Button>
                  <asp:Label id="Label2" style="Z-INDEX: 106; LEFT: 123px; POSITION: absolute; TOP: 56px" runat="server"
                        Height="23px" Font-Size="Large" Font-Bold="True" ForeColor="Black" BorderStyle="Solid"
                        Width="496px">In this example we save control in session variable and transfer it to a different window</asp:Label>
                  <asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 227px; POSITION: absolute; TOP: 10px" runat="server"
                        Width="214px" BorderStyle="Solid" ForeColor="Red" Font-Bold="True" Font-Size="Large" Height="18px">Transfer Control</asp:Label>&nbsp;
                  <asp:CheckBox ID="chkID" Runat="server" AutoPostBack="True" Text="Show City" Width="103px" style="Z-INDEX: 101; LEFT: 194px; POSITION: absolute; TOP: 132px"></asp:CheckBox>
                  <asp:CheckBox ID="chkTitle" Runat="server" AutoPostBack="True" Text="Show Company" style="Z-INDEX: 103; LEFT: 312px; POSITION: absolute; TOP: 132px"></asp:CheckBox>
                  <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" GridLines="Horizontal"
                        CellPadding="3" BackColor="White" BorderWidth="1px" BorderStyle="None" BorderColor="#E7E7FF"
                        style="Z-INDEX: 104; LEFT: 190px; POSITION: absolute; TOP: 165px">
                        <SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle>
                        <AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>
                        <ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
                        <HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
                        <FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
                        <Columns>
                              <asp:BoundColumn Visible="False" DataField="City" HeaderText="City"></asp:BoundColumn>
                              <asp:BoundColumn Visible="False" DataField="Company" HeaderText="Company"></asp:BoundColumn>
                              <asp:BoundColumn DataField="Country" HeaderText="Country"></asp:BoundColumn>
                              <asp:BoundColumn DataField="Product" HeaderText="Product"></asp:BoundColumn>
                              <asp:BoundColumn DataField="Quantity" HeaderText="Quantity"></asp:BoundColumn>
                        </Columns>
                        <PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>
                  </asp:DataGrid>
            </form>
      </body>
</HTML>
--------------------------------------------------------------------
Code behind
Imports System.Data.OleDb
Public Class TransferControl
    Inherits System.Web.UI.Page
    Dim da As OleDbDataAdapter

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.DataSet1 = New System.Data.DataSet
        CType(Me.DataSet1, System.ComponentModel.ISupportInitialize).BeginInit()
        '
        'DataSet1
        '
        Me.DataSet1.DataSetName = "NewDataSet"
        Me.DataSet1.Locale = New System.Globalization.CultureInfo("en-US")
        CType(Me.DataSet1, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub
    Protected WithEvents Hyperlink2 As System.Web.UI.WebControls.HyperLink
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents chkID As System.Web.UI.WebControls.CheckBox
    Protected WithEvents chkTitle As System.Web.UI.WebControls.CheckBox
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents DataSet1 As System.Data.DataSet
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

    '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.
        InitializeComponent()
    End Sub

#End Region

    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
        populateGridData()
    End Sub
    Private Sub populateGridData()
        Dim sConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
         "Data Source=C:\TestDB\TestDB.mdb;"

        Dim sSQL As String = "SELECT * FROM Test"
        Dim sSource As String = "Test"
        'Connect to database and specify sSQL
        da = New OleDbDataAdapter(sSQL, sConn)
        da.Fill(DataSet1, sSource) 'fsSource is a table


        bindgrid()
    End Sub

    Private Sub bindgrid()
        Dim dv As New DataView(DataSet1.Tables(0))
        With DataGrid1
            .DataSource = dv
            .DataBind()
        End With
    End Sub

    Private Sub chkID_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkID.CheckedChanged
        If chkID.Checked = True Then
            DataGrid1.Columns(0).Visible = True
        Else
            DataGrid1.Columns(0).Visible = False
        End If
        bindgrid()
    End Sub

    Private Sub chkTitle_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkTitle.CheckedChanged
        If chkTitle.Checked = True Then
            DataGrid1.Columns(1).Visible = True
        Else
            DataGrid1.Columns(1).Visible = False
        End If
        bindgrid()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Session.Add("grid", DataGrid1)
        Me.RegisterStartupScript("Progress", _
 "<script language='javascript'>window.open('TransferControlPopup.aspx', 0,'width=650, height=500, left=0,top=0,scrollbars=1,resize=yes,0');</script>")
    End Sub
End Class
=========================================================================
TransferControlPopup.aspx
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="TransferControlPopup.aspx.vb" Inherits="TestEverything.TransferControlPopup"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>TransferControlPopup</title>
            <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
            <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:Label id="Label2" style="Z-INDEX: 106; LEFT: 7px; POSITION: absolute; TOP: 2px" runat="server"
                        Height="23px" Font-Size="Large" Font-Bold="True" ForeColor="Black" BorderStyle="Solid"
                        Width="620px">Datagrid is created from session saved in the previous page</asp:Label>
            </form>
      </body>
</HTML>
------------------------------------------------------------------------------------------------
Code behind
Public Class TransferControlPopup
    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 Label2 As System.Web.UI.WebControls.Label

    '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.
        InitializeComponent()
    End Sub

#End Region

    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
        Dim O As Object = Session("grid")
        Page.Controls.Add(O)
    End Sub

End Class

0
 
puranik_pCommented:
@iboutchkine,
No need to create a popup for this.

@lvandevender,
Here the way to do it the way you want.
http://www.codeproject.com/aspnet/FreezePaneDatagrid.asp
0
 
lvandevenderAuthor Commented:
ok do i add that code in the html portion of visual studio?
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
puranik_pCommented:
No. Try to understand the code first before copying and pasting it.
You will find that its better to use the code from the link as it is and then replace the 'data to be displayed ' - code with your code.

Pura
0
 
puranik_pCommented:
I recommend: Accept puranik_p

Pura
0
 
puranik_pCommented:
lvandevender,
For one, you chose the wrong comment to accept as answer.
Another, you've given it a grade 'C'.
If you needed more assistance, I'd be glad to offer it.

I request you post a zero point question at http://www.experts-exchange.com/Community_Support/ to..
1. change the grade to better.
or..
2. 'Un'accept the comment as answer. I'd rather not get credit for this solution I suggested than getting a 'C' for it.

Thanks.
Pura
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now