# Persist Single-Row Selection on Virtual Scrolling in Grid

## Environment

<table>
	<tbody>
		<tr>
			<td>Product Version</td>
			<td>2022.2.621</td>
		</tr>
		<tr>
			<td>Product</td>
			<td>Grid for Progress® Telerik® UI for ASP.NET MVC</td>
		</tr>
	</tbody>
</table>

## Description

How can I persist the selected row in the Grid when the virtual scrolling functionality is enabled?

## Solution

The example below is implemented as per the following steps:

1. Handle the [`Change`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/grideventbuilder#changesystemstring) event of the Grid.
2. Use the [`select()` method](https://docs.telerik.com/kendo-ui/api/ui/grid/methods/select) to get the selected table row.
3. Get the data item of the selected row by using the [`dataItem()` method](https://docs.telerik.com/kendo-ui/api/ui/grid/methods/dataitem).
4. Use the private `_selectedIds` object of the Grid to save the selected row.

```Razor Index.cshtml
    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.OrderViewModel>()    
        .Name("grid")
        .Columns(columns => {
            columns.Bound(p => p.OrderID).Filterable(false).Width(100);
            columns.Bound(p => p.Freight).Width(100);
            columns.Bound(p => p.OrderDate).Format("{0:MM/dd/yyyy}").Width(140);
            columns.Bound(p => p.ShipName).Width(100);
            columns.Bound(p => p.ShipCity).Width(150);
        })
        .Events(ev => ev.Change("onChange"))
        .Pageable(p =>
            p.Info(true)
            .Numeric(false)
            .PreviousNext(false)
            .Messages(m=>m.Display("Showing {2} data items"))
        )
        .Scrollable(scrollable => scrollable.Virtual(true))
        .Selectable()
        .PersistSelection(true)
        .HtmlAttributes(new { style = "height:543px;" })
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(100)
            .Model(m =>
            {
                m.Id(p => p.OrderID);
            })
            .Read(read => read.Action("Orders_Read", "Grid"))
        )
    )
```

```JavaScript
    <script>
        function onChange(e) {
            var selectedRows = this.select();
            var dataItem = this.dataItem(selectedRows[0]);
            e.sender._selectedIds= {};
            e.sender._selectedIds[ dataItem.OrderID ]= true;
        }
    </script>
```

For a runnable example based on the code above, refer to [this REPL](https://netcorerepl.telerik.com/cGuAGClJ24i9B8RT11).

## More ASP.NET MVC Grid Resources

* [ASP.NET MVC Grid Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/overview)

* [ASP.NET MVC Grid Demos](https://demos.telerik.com/aspnet-mvc/grid/index)

* [ASP.NET MVC Grid Product Page](https://www.telerik.com/aspnet-mvc/grid)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Telerik REPL: Persist Single-Row Selection on Virtual Scrolling in Grid](https://netcorerepl.telerik.com/cGuAGClJ24i9B8RT11)
* [Client-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/grid)
* [Server-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/grid)
* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
