Web Grid Class is used to display the data in the Tabularform as well as to provide all the facilities like Paging, Sorting..etc.
Simply Web Grid Class acts like a GridView in the ASP.Net.
The following one is the namespace which contains the Web Grid Class.
Namespace
System.Web.Helpers
Description
Web Grid Class supports both types of View Engines.
They are
ASPX View Engine
Razor View Engine
The following are the properties of Web Grid Class.
Add a Controller with the name Home and write the following code in the HomeController.cs.
[csharp]
using WebGridExample.Models;
namespace WebGridExample.Controllers
{
public class EmployeeController : Controller
{
EmployeeEntities obj = new EmployeeEntities();
// GET: Employee
public ActionResult Index()
{
return View();
}
public ActionResult EmployeeView()
{
return View(obj.Employee.ToString());
}
}
}
[/csharp]
Run the Application and Check. Complete table data with sorting and Paging facility will be founded.
Alert
Note
By default, field Names are coming in Alphabetical order along with Sorting and Paging facilities.
If don't wanted to provide sorting and paging facilities, then at the time of creating web grid class object, one can use optional parameters like below.
CanPage:falseCanSort:false
Example
Example to create Web Grid with own columns in required order.
Create a new View page and write the following code.
[html]
<div>
@using(Html.Beginform())
{
@EmpGrid.GetHtml(columns:EmpGrid.Columns(EmpGrid.Column("EmpId"),EmpGrid.Column("EmpName"),EmpGrid.Column("EmpSalary")))
}
</div>
[/html]
Go to Controller class and write the following code.
[csharp]
using WebGridExample.Models;
namespace WebGridExample.Controllers
{
public class EmployeeController : Controller
{
EmployeeEntities1 obj = new EmployeeEntities1();
// GET: Employee
public ActionResult Index()
{
return View();
}
public ActionResult Sample()
{
return View(obj.Employee.ToString());
}
}
}
[/csharp]
If don't wanted to provide the sorting facility for the particular column. Then write the code as follows.
[csharp]
EmpGrid.Column("Ename",CanSort:false)
[/csharp]