How to perform custom paging with stored procedure on Grid in .NET MVC application?
This knowledge base document explains the way of how to perform custom paging with stored procedure.
- Render the Grid with Adaptor.
@(Html.EJ().Grid<object>("Editing") .Datasource(ds=> ds.URL("DataSource").Adaptor(AdaptorType.UrlAdaptor)) .AllowPaging() .PageSettings(p=> p.PageSize(10).CurrentPage(3)) .Columns(col => { col.Field("OrderID").Add(); col.Field("CustomerID").Add(); col.Field("Freight").Add(); col.Field("EmployeeID").Add(); }) ) |
- Create Stored Procedure to perform custom paging
CREATE PROCEDURE [dbo].[CustomPaging] @SkipCount int, @PageSize int, @TotalRows int output AS BEGIN SET NOCOUNT ON; SELECT @TotalRows = COUNT(*) FROM Orders
SELECT * FROM Orders ORDER BY OrderID OFFSET (@SkipCount) ROWS FETCH NEXT @PageSize ROWS ONLY
END
RETURN 0 |
- Pass the skip and take to the stored procedure from the controller.
public ActionResult DataSource(DataManager dm) { DataOperations operation = new DataOperations(); int count; DataTable dt = new DataTable("Order"); string constring = ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(constring)) { using (SqlCommand cmd = new SqlCommand("CustomPaging", con)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@SkipCount", dm.Skip); cmd.Parameters.AddWithValue("@PageSize", dm.Take); cmd.Parameters.Add("@TotalRows", SqlDbType.Int, 4); cmd.Parameters["@TotalRows"].Direction = ParameterDirection.Output; con.Open(); SqlDataReader dr = cmd.ExecuteReader(); dt.Load(dr); // Load into the dataTable dr.Close(); con.Close(); count = Convert.ToInt32(cmd.Parameters["@TotalRows"].Value); } } //Convet the dataTable into list List<EditableOrder> resList = new List<EditableOrder>(); resList = (from DataRow dr in dt.Rows select new EditableOrder() { OrderID = Convert.ToInt32(dr["OrderID"]), CustomerID = dr["CustomerID"].ToString(), EmployeeID = Convert.ToInt32(dr["EmployeeID"]), Freight = Convert.ToDecimal(dr["Freight"]), ShipCity = dr["ShipCity"].ToString()
}).ToList(); return Json(new { result = resList, count = count }, JsonRequestBehavior.AllowGet); } |
The following screenshot display the custom paging request and response.
Figure 1: Request header
Figure 2: Response header
Figure 3: Output
Conclusion
I hope you enjoyed learning about how to perform custom paging with stored procedure on Grid in .NET MVC application.
You can refer to our .NET MVC Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET MVC Grid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!