Search results

Scrolling in ASP.NET Core Grid control

22 Oct 2021 / 2 minutes to read

The scrollbar will be displayed in the grid when content exceeds the element width or height. The vertical and horizontal scrollbars will be displayed based on the following criteria:

  • The vertical scrollbar appears when the total height of rows present in the grid exceeds its element height.
  • The horizontal scrollbar appears when the sum of columns width exceeds the grid element width.
  • The height and width are used to set the grid height and width, respectively.

The default value for height and width is auto.

Set width and height

To specify the width and height of the scroller in the pixel, set the pixel value to a number.

tagHelper
width-height.cs
Copied to clipboard
<ejs-grid id="Grid" dataSource="@ViewBag.datasource"  height="315" width="400" > 
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
        <e-grid-column field="EmployeeID" headerText="Employee ID" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>
Copied to clipboard
public IActionResult Index()
 {
   var orders = OrderDetails.GetAllRecords();
   ViewBag.datasource = orders;            
   return View();
 }

Responsive with parent container

Specify the width and height as 100% to make the grid element fill its parent container. Setting the height to 100% requires the grid parent element to have explicit height.

tagHelper
responsive-parent.cs
Copied to clipboard
<ejs-grid id="Grid" dataSource="@ViewBag.datasource" height="100%"  > 
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
        <e-grid-column field="EmployeeID" headerText="Employee ID" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>
Copied to clipboard
public IActionResult Index()
 {
   var orders = OrderDetails.GetAllRecords();
   ViewBag.datasource = orders;            
   return View();
 }

Scroll to selected row

You can scroll the grid content to the selected row position by using the rowSelected event.

tagHelper
scroll-selected-row.cs
Copied to clipboard
<ejs-numerictextbox id="numeric" width="200" min="0" showSpinButton="false" placeholder="Enter index to select a row" change="onChange"></ejs-numerictextbox>
<ejs-grid id="Grid" dataSource="@ViewBag.datasource" rowSelected="rowSelected" height="270" width="100%">
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" width="140"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" width="120"></e-grid-column>
        <e-grid-column field="OrderDate" headerText="Order Date" textAlign="Right" format="yMd" width="140"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>

    function onChange(args) {
        var gridObj = document.getElementById("Grid").ej2_instances[0];
        gridObj.selectRow(parseInt(this.getText(), 10));
    }

    function rowSelected(args) {
        var rowHeight = this.getRows()[this.getSelectedRowIndexes()[0]].scrollHeight;
        this.getContent().children[0].scrollTop = rowHeight * this.getSelectedRowIndexes()[0];
    }

</script>
Copied to clipboard
public IActionResult Index()
 {
   var orders = OrderDetails.GetAllRecords();
   ViewBag.datasource = orders;            
   return View();
 }

Hide the scrollbar when the content is not overflown

You can hide the scrollbar of Grid content by using the hideScroll method when the content doesn’t overflow its parent element.

In the following sample, we have invoked the hideScroll method inside the dataBound event.

tagHelper
hide-scroll.cs
Copied to clipboard
<ejs-grid id="Grid" dataSource="@ViewBag.datasource" dataBound="dataBound" height="400" width="100%" allowPaging="true"> 
    <e-grid-pagesettings pageSize=2>
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" width="140"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" width="120"></e-grid-column>
        <e-grid-column field="OrderDate" headerText="Order Date" textAlign="Right" format="yMd" width="140"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>

    function dataBound() {
        var grid = document.getElementById('Grid').ej2_instances[0];
        grid.hideScroll();
    }

</script>
Copied to clipboard
public IActionResult Index()
 {
   var orders = OrderDetails.GetAllRecords();
   ViewBag.datasource = orders;            
   return View();
 }

You can make the Grid column headers remain fixed while scrolling by using the enableStickyHeader property.

In the below demo, the Grid headers will be sticky while scrolling the Grid’s parent div element.

tagHelper
sticky-header.cs
Copied to clipboard
<div style='height:350px;'>
<ejs-grid id="Grid" dataSource="@ViewBag.datasource" enableStickyHeader="true"> 
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" width="140"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" width="120"></e-grid-column>
        <e-grid-column field="OrderDate" headerText="Order Date" textAlign="Right" format="yMd" width="140"></e-grid-column>
    </e-grid-columns>
</ejs-grid>
</div>
Copied to clipboard
public IActionResult Index()
 {
   var orders = OrderDetails.GetAllRecords();
   ViewBag.datasource = orders;            
   return View();
 }