How to scroll the Grid Scroller programmatically in ASP.NET MVC?
This Knowledge base explains how to scroll the ASP.NET MVC Grid scroller programmatically.
Solution:
We can achieve this by using getScrollObject method of ejGrid and scrollY method of ejScroller. The getScrollObject method returns the instance of the scroller object.
In scrollY method we need to pass the integer value to move the scroller. In the below given solution we have moved the grid scroller to the end of the Grid based on the length of the current view data and row height.
1.Render the Grid control.
HTML
<input type="button" value="MoveScroller" onclick="moveScroller()" /> <div id="Grid"></div>
JavaScript
<script> $(function () { $("#Grid").ejGrid({ // the datasource "window.gridData" is referred from 'http://js.syncfusion.com/demos/web/scripts/jsondata.min.js' dataSource: window.gridData, allowPaging: true, allowScrolling: true, scrollSettings: {height:300, width:600}, columns: [ { field: "OrderID", headerText: "Order ID", width: 75 }, { field: "CustomerID", headerText: "Customer ID", width: 75 }, { field: "EmployeeID", headerText: "Employee ID", width: 75 }, { field: "ShipCity", headerText: "ShipCity", width: 110 } ] }); }); </script>
MVC
<input type="button" value="MoveScroller" onclick="moveScroller()" /> @(Html.EJ().Grid<object>("Grid") .Datasource((IEnumerable<object>)ViewBag.datasource) .AllowPaging() .AllowScrolling() .ScrollSettings(scroll => {scroll.Height(300).Width(600); } ) .Columns(col => { col.Field("OrderID").HeaderText("Order ID").Width(75).Add(); col.Field("CustomerID").HeaderText("Customer ID").Width(75).Add(); col.Field("EmployeeID").HeaderText("Employee ID").Width(75).Add(); col.Field("ShipCity").HeaderText("ShipCity").Width(110).Add(); }))
C#
using EJGrid.Models; using System.Linq; using System.Web.Mvc; using System.Web.UI.WebControls; using Syncfusion.Linq; namespace EJGrid.Controllers { public class HomeController : Controller { public ActionResult Index() { ViewBag.datasource = OrderRepository.GetAllRecords().ToList(); return View(); } } }
ASPX
<input type="button" value="MoveScroller" onclick="moveScroller()" /> <ej:Grid runat="server" ID="FlatGrid" AllowPaging="true" AllowScrolling="true" > <ScrollSettings Height="300" Width="600" /> <Columns> <ej:Column Field="OrderID" HeaderText="Order ID" Width="75" /> <ej:Column Field="CustomerID" HeaderText="Customer ID" Width="75" /> <ej:Column Field="EmployeeID" HeaderText="Employee ID" Width="75" /> <ej:Column Field="ShipCity" HeaderText="ShipCity" Width="110" /> </Columns> </ej:Grid>
C#
using System; using System.Linq; using System.Web.UI; using Syncfusion.Linq; namespace WebApplication21 { public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { this.FlatGrid.DataSource = OrderRepository.GetAllRecords().ToList(); this.FlatGrid.DataBind(); } } }
ASP.NET CORE
<input type="button" value="MoveScroller" onclick="moveScroller()" /> <ej-grid id="FlatGrid" datasource="ViewBag.datasource" allow-paging="true" allow-scrolling=true > <e-scroll-settings height="300" width="600"></e-scroll-settings> <e-columns> <e-column field="OrderID" header-text="Order ID" width="75" ></e-column> <e-column field="CustomerID" header-text="Customer ID" width="75"></e-column> <e-column field="EmployeeID" header-text="Employee ID" width="75"></e-column> <e-column field="ShipCity" header-text="ShipCity" width="110"></e-column> </e-columns> </ej-grid>
C#
using System.Linq; using Microsoft.AspNetCore.Mvc; using WebApplication1.Models; namespace WebApplication1.Controllers { public class HomeController : Controller { private NORTHWNDContext _context; public HomeController(NORTHWNDContext context) { _context = context; } public IActionResult Index() { ViewBag.datasource = _context.Orders.ToList(); return View(); } } }
Angular
HTML
<input type="button" ej-button id="button" value="MoveScroller" (ejclick)="moveScroller($event)" /> <ej-grid id="Grid" [dataSource]="gridData" [allowScrolling]="true" [scrollSettings]="scrollSettings > <e-columns> <e-column field="OrderID" headerText="Order ID" width="75" ></e-column> <e-column field="CustomerID" headerText="Customer ID" width="75"></e-column> <e-column field="EmployeeID" headerText="Employee ID" width="75" ></e-column> <e-column field="ShipCity" headerText="ShipCity" width="110"></e-column> </e-columns> </ej-grid>
TS
export class GridComponent { public gridData; public scrollSettings; @ViewChild('Grid') Grid: EJComponents<any, any>; constructor() { // the datasource "window.gridData" is referred from 'http://js.syncfusion.com/demos/web/scripts/jsondata.min.js' this.gridData = window.gridData; this.scrollSettings = { width: 600, height: 300 }; } moveScroller (e: any){ var grid = $("#Grid").data("ejGrid"); grid.getScrollObject().scrollY(grid.getRowHeight() * grid.model.currentViewData.length); } }
2. In a button click event we have obtained the instance of grid scroller using getScrollObject method. Then we have moved the scroller to the end of the Grid using scrollY method based on the values of row height and current view data length.
JS
<script> function moveScroller (args) { var grid = $(".e-grid").ejGrid("instance"); grid.getScrollObject().scrollY(grid.getRowHeight() * grid.model.currentViewData.length) } </script>
Result:
Figure 1.Before moving the scroller
Figure 2.After moving the scroller
Conclusion
I hope you enjoyed learning about how to scroll the Grid Scroller programmatically in ASP.NET MVC.
You can refer to our ASP.NET MVC Grid feature tour page to know about its other groundbreaking feature representations. You can also explore our ASP.NET MVC Grid examples to understand how to present and manipulate data.
For current customers, you can check out our ASP.NET MVC Controls from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our ASP.NET MVC DataGrid and other ASP.NET MVC controls.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!