|
| 1 | +--- |
| 2 | +title: Manual Data Source Operations |
| 3 | +page_title: Grid for Blazor | Manual Operations |
| 4 | +description: How to implement your own read, page, fiter, sort operations for the grid data. |
| 5 | +slug: components/grid/manual-operations |
| 6 | +tags: telerik,blazor,grid,read,filter,sort,page,manual,data,data source |
| 7 | +published: True |
| 8 | +position: 30 |
| 9 | +--- |
| 10 | + |
| 11 | +# Manual Data Source Operations |
| 12 | + |
| 13 | +By default, the grid will receive the entire collection of data, and it will perform the necessary operations (like [paging]({%slug components/grid/features/paging%}), [sorting]({%slug components/grid/features/sorting%}), [filtering]({%slug components/grid/filtering%})) internally to it. You can perform these operations yourself by handling the `OnRead` event of the grid as shown in the example below. The data source will be read after each [CUD operation]({%slug components/grid/editing/overview%}) as well, to ensure fresh data. |
| 14 | + |
| 15 | +The parameter of type `DataSourceRequest` exposes information about the desired paging, filtering and sorting so you can, for example, call your remote endpoint with appropriate parameters so its performance is optimized and it fetches only the relevant data. |
| 16 | + |
| 17 | +When the `OnRead` event is used, the internal operations are disabled and you must perform them all in the `OnRead` event. You must also set the `TotalCount` property of the grid to the total number of items in the data source. |
| 18 | + |
| 19 | +>caption Handling the data source operations with your own code. This example showcases how to use the OnRead event. To implement the CUD operations, see the [CRUD Operations Overview]({%slug components/grid/editing/overview%}) article. |
| 20 | +
|
| 21 | +````CSHTML |
| 22 | +@using Telerik.Blazor.Components.Grid |
| 23 | +@using Telerik.DataSource.Extensions; |
| 24 | +
|
| 25 | +<TelerikGrid Data=@GridData TotalCount=@Total |
| 26 | + Filterable=true Sortable=true Pageable=true EditMode="inline"> |
| 27 | + <TelerikGridEvents> |
| 28 | + <EventsManager OnRead=@ReadItems></EventsManager> |
| 29 | + </TelerikGridEvents> |
| 30 | + <TelerikGridColumns> |
| 31 | + <TelerikGridColumn Field=@nameof(Employee.ID) /> |
| 32 | + <TelerikGridColumn Field=@nameof(Employee.Name) Title="Name" /> |
| 33 | + <TelerikGridColumn Field=@nameof(Employee.HireDate) Title="Hire Date" /> |
| 34 | + <TelerikGridCommandColumn> |
| 35 | + <TelerikGridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</TelerikGridCommandButton> |
| 36 | + <TelerikGridCommandButton Command="Edit" Icon="edit">Edit</TelerikGridCommandButton> |
| 37 | + <TelerikGridCommandButton Command="Delete" Icon="delete">Delete</TelerikGridCommandButton> |
| 38 | + <TelerikGridCommandButton Command="Cancel" Icon="cancel" ShowInEdit="true">Cancel</TelerikGridCommandButton> |
| 39 | + </TelerikGridCommandColumn> |
| 40 | + </TelerikGridColumns> |
| 41 | + <TelerikGridToolBar> |
| 42 | + <TelerikGridCommandButton Command="Add" Icon="add">Add Employee</TelerikGridCommandButton> |
| 43 | + </TelerikGridToolBar> |
| 44 | +</TelerikGrid> |
| 45 | +
|
| 46 | +@functions { |
| 47 | + public List<Employee> SourceData { get; set; } |
| 48 | + public List<Employee> GridData { get; set; } |
| 49 | + public int Total { get; set; } = 0; |
| 50 | +
|
| 51 | + protected override void OnInit() |
| 52 | + { |
| 53 | + SourceData = GenerateData(); |
| 54 | + } |
| 55 | +
|
| 56 | + protected async void ReadItems(GridReadEventArgs args) |
| 57 | + { |
| 58 | + Console.WriteLine("data requested: " + args.Request); |
| 59 | +
|
| 60 | + //you need to update the total and data variables |
| 61 | + //the ToDataSourceResult() extension method can be used to perform the operations over the full data collection |
| 62 | + //in a real case, you can call data access layer and remote services here instead, to fetch only the necessary data |
| 63 | +
|
| 64 | + var datasourceResult = SourceData.ToDataSourceResult(args.Request); |
| 65 | +
|
| 66 | + GridData = (datasourceResult.Data as IEnumerable<Employee>).ToList(); |
| 67 | + Total = datasourceResult.Total; |
| 68 | +
|
| 69 | + StateHasChanged(); |
| 70 | + } |
| 71 | + |
| 72 | + //This sample implements only reading of the data. To add the rest of the CRUD operations see |
| 73 | + //https://docs.telerik.com/blazor-ui/components/grid/editing/overview |
| 74 | +
|
| 75 | + private List<Employee> GenerateData() |
| 76 | + { |
| 77 | + var result = new List<Employee>(); |
| 78 | + var rand = new Random(); |
| 79 | + for (int i = 0; i < 100; i++) |
| 80 | + { |
| 81 | + result.Add(new Employee() |
| 82 | + { |
| 83 | + ID = i, |
| 84 | + Name = "Name " + i, |
| 85 | + HireDate = DateTime.Now.Date.AddDays(rand.Next(-20, 20)) |
| 86 | + }); |
| 87 | + } |
| 88 | +
|
| 89 | + return result; |
| 90 | + } |
| 91 | +
|
| 92 | + public class Employee |
| 93 | + { |
| 94 | + public int ID { get; set; } |
| 95 | + public string Name { get; set; } |
| 96 | + public DateTime HireDate { get; set; } |
| 97 | + } |
| 98 | +} |
| 99 | +```` |
| 100 | + |
| 101 | +## See Also |
| 102 | + |
| 103 | + * [CRUD Operations Overview]({%slug components/grid/editing/overview%}) |
| 104 | + * [Live Demo: Manual Data Source Operations](https://demos.telerik.com/blazor-ui/grid/manual-operations) |
| 105 | + |
0 commit comments