Lock the first columns and rows of an ASP.NET GridView

My goal is to freeze columns 0 and 1 while also freezing rows 0, 1, and 2 (excluding the header) when scrolling both vertically and horizontally. I attempted to achieve this with jQuery code without success, so I turned to a CSS example from Stack Overflow for my project. Currently, I am focusing on locking the columns only but eventually, I will need to lock the rows as well.

Within an HTML table, I have an ASP.NET C# gridview that populates with data from a SQL database upon page load.

I successfully apply CSS formatting to the grid during the necessary events.

To illustrate, here is a simplified representation of how the grid looks:

You may not see the horizontal scroll bar due to the number of years selected by the user, which can vary depending on their choice.

Below is the recent CSS I implemented:

.pinned
{
    position: fixed; 
    background-color: #FFFFFF; 
    z-index: 100;
}

.scrolled
{
    position: relative;
    left: 150px; 
    overflow: hidden;
    white-space: nowrap;
    min-width: 50px; 
}

.col1
{
    left: 0px;
    width: 50px;
}

.col2
{
    left: 50px;  
    width: 100px;
}

Here is a snippet of the HTML structure used:

<div style="overflow:scroll; height: 400px; width: 911px; margin:auto;">
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" GridLines="Vertical" OnRowCreated="COl" OnRowDataBound="ROWCSS" ShowHeader="false"> 

 </asp:GridView> 
</div>

This C# function formats the rows based on certain conditions:

protected void ROWCSS(object sender, GridViewRowEventArgs e)
{
   // Code logic here...
}

Similarly, the following C# code controls the column locking operation:

protected void COl(object sender, GridViewRowEventArgs e)
{
   // Code logic here...
}

The outcome of applying these changes sometimes results in unexpected visual issues within the first three rows. These anomalies might be related to the colspan functionality triggered in a separate event.

By combining CSS classes like naranjaCSSLEFT, azulCSSRIGHT, negroCSSLEFT, and FILACSS, I set different styles for fonts and backgrounds in specific scenarios.

In conclusion, despite efforts to freeze the columns, the desired effect has not been achieved yet.

Answer №1

After continuing to work on this project last night, I managed to find a solution for columns 0 and 1. However, I still need to figure out how to freeze the first three rows (0, 1, 2). The current example I am working on freezes the header, but my intention is to hide it.

This is the updated code snippet I am implementing:

HTML:

   <div>
       <asp:GridView ID="cphMain_cphMain_cphMain_GridView2" runat="server" Width="100%" AutoGenerateColumns="true" GridLines="None" 
           OnRowCreated="COl"   OnRowDataBound="ROWCSS">  
       <RowStyle HorizontalAlign="Left" VerticalAlign="Middle" Wrap="False" />

    </asp:GridView>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#cphMain_cphMain_cphMain_GridView2').gridviewScroll({
                width: 930,
                height: 400,
                freezesize: 2
            });
        });
     </script>
   </div> 

In the "head" section, I have included the following script URLs:

HTML :

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script> 
<script src="JQuery/GridViewHeader.js"></script>

To style the grid using CSS formatting, I am utilizing the same events mentioned earlier, with additional CSS extensions such as Pinned, col1, col2, scrolled.

The following code segment is no longer being used:

//THIS IS THE FOR, THAT LOCKS THE COLUMN 0 and 1 ************

for (int j = 0; j <= (i - 1); j++)
{

    if (j == 0)
    {
        e.Row.Cells[j].CssClass = "pinned col1";
    }
    else if (j == 1)
    {
        e.Row.Cells[j].CssClass = "pinned col2";
    }

    else
    {
        e.Row.Cells[j].CssClass = "scrolled";
    }

}

//********************************

If you are curious about the contents of the script referenced in:

<script src="JQuery/GridViewHeader.js"></script>

You can review it here (admittedly, I cannot decipher it myself as I simply copied and pasted it):

I sourced this example from:

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

retrieve the data received through ajax

Currently, I am dealing with the following situation: <form method="POST" name="f"> <input type="file" name="FileUpload" id="FileUpload" /> </form> I am utilizing ajax to send the file using the upload.file method as shown below ...

Show a preloader using Jquery after all div elements have been fetched

My goal is to have a preloader displayed in a div, and once another div has finished loading its content, I want the preloader to disappear. Here is an example: <div id="container"> <div id="preloader"> Loading.... </div> ...

Generating a new text file based on user input in C#

Hi there, I'm working on a webform and I need to allow the user to enter the name of the text file they want to create in a specific directory. However, I'm having trouble figuring out how to do this. Can anyone help me out? The current code cre ...

"Combining variables in programming: A step-by-step guide with a sample

When I press the first button, I expect to see random numbers displayed: 817 754 692 Pressing the second button '123:123' should show the numbers like this: 817:817 754:754 However, the output I am getting is: 817 754 :817 754 How can I com ...

Using jQuery to send both the selected file contents and parameters in an ajax request

Currently, I am utilizing this code snippet to transmit the selected file's contents to the server: <!DOCTYPE html> <html lang="en> <head> <title>Test</title></head> <script src="https://ajax.googleapis.com/ ...

Discover the top "Most Popular" links/pages on a website

I have several tables that store information. Users can click on the title to view more details. Now, I would like to display a list of the "Most Popular" links (based on hits) on the homepage. Since I am new to this concept, I am not sure how to achieve ...

Navigable Features and Regions in .NET utilizing GDI+

Struggling to grasp the proper method for creating a scrollable region within a custom control using C#, .NET 3.5, and GDI+. Are there any resources from Microsoft that outline the correct approach? Any informative guides available online? I'm parti ...

Tips on switching the positions of two links when they are clicked using jQuery

<div class="applications-wrapper"> <div class="eye" id="onetwo"><a class="fa fa-eye" href="#"></a></div> <div class="bar" id="twothree"><a class="fa fa-bars" href="#"></a></div> </div> My ...

Using jQuery to enhance the functionality of the drop-down select feature

I am facing an issue with my index file code. When I select something from the drop-down menu, I expect to see a related dropdown appear. Although I have added this code to my file, I am unable to get the drop down to show after selecting the main type. ...

What causes the off-canvas menu to displace my fixed div when it is opened?

Using the Pushy off-canvas menu from GitHub for my website has been great, but it's causing some trouble with my fixed header. When I scroll down the page, the header sticks perfectly to the top, but once I open the off-canvas menu, the header disappe ...

Dynamic image uploading with Ajax

One issue I've encountered is with the ajax image upload feature on my form. It uploads the image when triggered by the onChange event for the file input, but then it uploads again when submitting the form. Is there a way to prevent this duplicate upl ...

Is it possible to create a new record with a Web API POST Request where all attributes are intentionally left blank?

When making a Web API POST Request to add a new record to a specific table using JSON attributes from the body, why are all the attributes ending up as NULL? I have checked and entered the correct attributes, but still facing this issue. Below is the snip ...

What is the best way to retrieve a date from within an array using jQuery?

Trying to extract a date value from a JSON object, but the date is nested inside an array. Sample code below fetch.php <?php include_once('connect.php'); $employee_id = $_POST['employee_id']; $query = "SELECT ...

Adaptable background images with CSS styling

I'm attempting to adjust the sizing of my background image on the splash page to make it smaller. My goal is for the image to fill the entire section regardless of screen size. .splash{ background-image: url("../images/lanternboys_medium.jpg"); backg ...

Avoiding server requests in Firefox by refraining from using ajax

I've implemented the following jquery code: $("#tasksViewType").selectBox().change( function (){ var userId = $('#hiddenUserId').val(); var viewTypeId = $("#tasksViewType").val(); $.post('updateViewType& ...

I created a website on a Windows operating system, but when I opened it on Linux, it was completely distorted. What

While honing my skills in HTML and Bootstrap, I decided to clone a website. I painstakingly added images and overlays for all the images within my Windows environment. However, when I switched my programming environment to Linux Mint, I encountered some i ...

Unable to delete the Microsoft.Bcl.Build.Tasks.dll file

I'm currently facing an issue with my ASP.NET Web-API project where the build agent is unable to clean its working directories due to the fact that the library Microsoft.Bcl.Build.Tasks.dll is still being used by a process, preventing it from being de ...

What occurs when a non-static class contains a static class? How is memory management impacted by this structure?

When a static class is nested inside a non-static class, how does memory management work? public class test { public static class stringVariables { // inline code } // inline code } Can the garbage collector collect the object of the ...

Issues arise when using the Jquery addClass method as it may not

Recently delving into Jquery, I encountered an issue. My goal is to click on the send button and have the input's border turn red. Here's the HTML code: <input type="text" placeholder="Put your name" id="name"/> <input type="submit" va ...

Parallel Bootstrap vertical grid layout

Struggling to articulate my objective, but bear with me. I am in the process of creating two bootstrap grids to house expanding text boxes. Refer to the image below for a visual representation; Desired Outcome Despite my efforts, the output from my code ...