Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
In MVC4 application I can able to bind the database table columns into the Grid Header. But I need to bind the database table columns values as a Grid header. Is this possible in MVC?
Posted
Comments
Jameel VM 4-Aug-14 1:05am    
please post the code you had tried
ilaya muthumanickam 4-Aug-14 2:08am    
i had tried the single table like tbl_student tghe controller and View codes are Given Below

Controller.cs

namespace WEBGRIDinSchool.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
School3651Entities db = new School3651Entities();
public ActionResult Index()
{



School3651Entities db = new School3651Entities();
int insid = 1113;
var Query = (from x in db.tbl_student

select x).ToList();
ViewBag.query = Query;



return View(ViewBag.query);
}
View:
@{
ViewBag.Title = "Index";
}

@*<script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>*@
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<style type="text/css">
.webGrid { margin: 60px; border-collapse: collapse; width: 500px; background-color:#6D6552;}
.header { background-color: #20364B; font-weight: bold; color: #FFF; }
.webGrid th, .webGrid td { border: 1px solid #C0C0C0; padding: 5px; }
.alt { background-color: #E4E9F5; color: #000; }
.gridHead a:hover {text-decoration:underline;}
.description { width:auto}
.select{background-color: #389DF5}
</style>

@{

var grid = new WebGrid(ViewBag.query, canPage: true, rowsPerPage:10, selectionFieldName: "selectedRow", ajaxUpdateContainerId: "gridContent");
grid.Pager(WebGridPagerModes.NextPrevious);}

<div id="gridContent">

@grid.GetHtml(tableStyle: "webGrid",
headerStyle: "header",
alternatingRowStyle: "alt",
selectedRowStyle: "select",
columns: grid.Columns(
grid.Column("firstname", "firstname", style: "description"),
grid.Column("lastname", "lastname", style: "description"),
grid.Column("middlename", "middlename"),
grid.Column("gender", "gender"),
grid.Column("dateofadmission", "dateofadmission"),
grid.Column("dateofbirth", "dateofbirth"),



grid.Column(format: (item) => Html.ActionLink("Edit", "ActionName", new { param1 = item.institutionid }))





))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900