Adjust the header width and column alignment when freezing the header in a gridview interface

Looking to implement a gridview with a fixed header? I've tried various solutions including this link and this one.

While I've managed to get it working, the main issue lies in the alignment of header width and column width. The scroll and freeze functionality works fine, but visually there's an inconsistency between the header and columns. It seems like when I don't use the freeze logic, the header width appears correctly.

Upon comparing the provided code with mine, I noticed that they defined columns directly in the design, whereas I set them through SQL data source in the code behind. This difference might be causing the width discrepancy.

<form id="form1" runat="server">
  <div>

  </div>

   <asp:GridView ID="GridView1" runat="server" Font-Size=12px CellPadding="4" 
    ForeColor="#333333" GridLines="Vertical">

   <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#999999" />
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
   </asp:GridView>

</form>

A screenshot clearly illustrates the highlighted problem (notice the disparity in widths).

Could the absence of <th> tags and column names being explicitly specified be the root cause?

If so, how can this issue be resolved? How can I ensure that the header width matches the column width accurately?

Edit:

The rendered HTML page shows some discrepancies compared to the ASP.NET page viewed in the browser. Even with slight differences, the header width issue persists in both versions.

(HTML content)

Answer №1

If you are looking for an alternative solution, check out this link:

This method has been effective in my experience.

Answer №2

I have implemented a customized version of the MakeStaticHeader function:

function CreateStickyHeader(gridId, includeFooter) {
var height = $('#' + gridId).height();
var table = document.getElementById(gridId);
if (table) {
    var headerHeight = $('#' + gridId + ' tr:first-child').height();
    var HeaderRowDiv = document.getElementById(gridId + 'DivHeaderRow');
    var MainContentDiv = document.getElementById(gridId + 'DivMainContent');
    var FooterRowDiv = document.getElementById(gridId + 'DivFooterRow');

    //*** Set Header Row Div Properties ****
    HeaderRowDiv.style.height = headerHeight + 'px';
    HeaderRowDiv.style.position = 'relative';
    HeaderRowDiv.style.top = '0px';
    HeaderRowDiv.style.zIndex = '10';
    HeaderRowDiv.style.verticalAlign = 'top';
    HeaderRowDiv.style.marginRight = '17px';

    //*** Set Main Content Div Properties ****
    MainContentDiv.style.height = height + 'px';
    MainContentDiv.style.position = 'relative';
    MainContentDiv.style.top = -headerHeight + 'px';
    MainContentDiv.style.zIndex = '1';

    //*** Set Footer Row Div Properties ****
    FooterRowDiv.style.position = 'relative';
    FooterRowDiv.style.top = -headerHeight + 'px';
    FooterRowDiv.style.verticalAlign = 'top';
    FooterRowDiv.style.paddingTop = '2px';

    if (includeFooter) {
        var clonedTable = table.cloneNode(true);
        clonedTable.removeChild(clonedTable.getElementsByTagName('tbody')[0]);
        var tableBody = document.createElement('tbody');
        tableBody.appendChild(table.rows[table.rows.length - 1]);
        clonedTable.appendChild(tableBody);
        FooterRowDiv.appendChild(clonedTable);
    }

    //****Copy Header into Header Row Div****
    HeaderRowDiv.appendChild(table.cloneNode(true));
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a Form Layout with Bootstrap - Organizing Text Boxes and Text Areas

https://i.sstatic.net/oqRwR.jpg In the image above, I need to adjust the position of the textbox to align it with the TextArea. Is there a Bootstrap class that can help me achieve this? My current version of Bootstrap is 3.3.6. HTML &l ...

`A dynamically captivating banner featuring animated visuals created with JQuery`

I am currently in the process of designing a banner for the top of my webpage, but I have yet to come across any code that meets all my requirements. To provide clarification, I have attached an illustration showcasing what I am aiming to achieve. 1) The ...

Experiencing Difficulty Retaining Checkbox State Using LocalStorage Upon Page Reload

At the moment, I have a script that automatically clicks on a random checkbox whenever the page loads or refreshes. Through localStorage, I can also view the value of the input assigned to the randomly selected checkbox. However, I'm facing an issue ...

Tips for adjusting the header color in materialize framework?

I've been working on a web template and I'm looking to customize the color displayed in the Android browser (maybe using JS?). The default color is blue. How can I go about changing it? https://i.sstatic.net/RxLbS.jpg Appreciate any help! ...

Unable to retrieve the chosen option from the datalist

I am encountering an issue with a datalist where I can't seem to retrieve the value that is currently selected when a button is clicked. Despite following recommendations from various posts on Stack Overflow, my code still returns undefined. Interesti ...

Ways to showcase tooltip text for an unordered list item?

When creating an unordered list, each element's text corresponds to a chapter name. I also want to include the description of each chapter as tooltip text. The Javascript code I currently have for generating a list item is: var list_item = document.c ...

Avoid assigning a class name to child elements if both the parent and child elements already have inline background colors

I have a challenge where I need to assign a random class name to elements that have an inline background color. The additional condition is that if both the parent and child elements have inline background colors, the child element should not receive the c ...

The Art of Revealing an Element

Is it possible to manipulate a parent div element in JavaScript without affecting its child nodes? For example, transforming this structure: <div class="parent"> <div class="child"> <div class="grandchil ...

What is the best way to conceal the border and background color of the dropdown arrow in an HTML <select> tag?

Upon browsing the Mozilla homepage, I noticed a peculiar element in the center - a <select> option with an arrow lacking border and background color. In contrast, when visiting the Facebook sign-up page, the drop-down arrow adheres to the Windows sta ...

The HTML component fails to acknowledge width settings

I seem to be having trouble identifying an issue or perhaps I am misunderstanding something (I am using daisyui + nx + tailwind css + angular). To simplify my problem, let's consider the following scenarios: In the first scenario, I have: <div cl ...

Using Jquery to add new HTML content and assign an ID to a variable

Here is some javascript code that I am having trouble with: var newAmount = parseInt(amount) var price = data[0]['Product']['pris']; var id = data[0]['Product']['id']; var dat ...

Using jQuery AJAX to Transfer Data to a PHP Script

There is a div element with a data-id attribute. The goal is to use jQuery to detect when this div is clicked and then send or store the corresponding data-id value in a PHP file. However, the code provided raises an error when trying to obtain the data-id ...

creating an HTML table from JSON data using a specific key

In this json file, I am looking to create separate tables based on the IDs provided. For example, ID: "50" should be displayed in one table, while ID: "57" should be shown in another table within the same context. { "version": "5.2", "user_type": ...

JavaScript code that uses jQuery does not function properly on an HTML form

Hello everyone, I am having trouble with some JavaScript code. Here is what I have: $(document).ready(function(){ $(".replyLink").click(function(){ $("#form-to-"+this.id).html(htmlForm()).toggle(500); return false; }); function htmlForm(){ var htm ...

Transferring the link value to an AJAX function when the onclick event is triggered

I have a link containing some data. For example: <li><a href="" onclick="getcategory(this);"><?php echo $result22['category']; ?></a></li> I need this link to pass the value of $result22['category']; to ...

What is the process for sending a request and obtaining a response from a REST API using JavaScript?

I have a private server set up on my computer with a built-in REST API. The base URL for the API is: . I am now looking to develop a client application for this server using the API. To authenticate on the server, the API endpoint is baseurl/login with the ...

Does CSS delayed visibility transition fail to activate JavaScript transitionend event for elements' visibility changes?

My current approach involves using a clever method to delay the transition of visibility, in combination with opacity. So far, everything is working smoothly: <body> <button>run</button> <div class="box"></div> & ...

What is the correct way to incorporate scrollIntoView() using jQuery?

I'm trying to implement a jQuery function that will enable the last reply to scroll into view. This is my current code: function displayLastReply(replies){ replies.each(function() { if($(this).index() < nIniRep || afterReply){ $( ...

I created a news platform using Next.js server-side rendering, but I'm having trouble with the export const metadata = { title: "TEST" } not functioning as intended

After building my News Website using Next.js with Server Side Rendering, I encountered an issue with setting the page title. Despite using export const metadata = { title: "TEST" }, the title remained unchanged. The metadata object appears to be ...

I am looking to dynamically insert a text field into an HTML form using jQuery or JavaScript when a specific button is clicked

This is my code snippet: <div class="rButtons"> <input type="radio" name="numbers" value="10" />10 <input type="radio" name="numbers" value="20" />20 <input type="radio" name="numbers" value="other" />other </div> ...