var db =Database.Open("WebPagesMovies");
var selectedData = db.Query("SELECT * FROM Movies");
var grid =newWebGrid(source: selectedData);
@{ var db=Database.Open("Empty Site"); var data=db.Query("select * from users"); var grid=new WebGrid(source:data); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title></title> </head> <body> @grid.GetHtml() </body> </html>
如果要自定义样式: @grid.GetHtml( columns: grid.Columns( grid.Column("Title"), grid.Column("Genre"), grid.Column("Year") ) )
@grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("Title"),
grid.Column("Genre"),
grid.Column("Year")
)
)
如果要分页:
var grid =newWebGrid(source: selectedData, rowsPerPage:3);
完整页面代码:
@{
var db = Database.Open("WebPagesMovies");
var selectedData = db.Query("SELECT * FROM Movies");
var grid = new WebGrid(source: selectedData, rowsPerPage: 3);
}
<!DOCTYPE html>
<htmllang="en">
<head>
<metacharset="utf-8"/>
<title>Movies</title>
<styletype="text/css">
.grid {margin:4px;border-collapse: collapse;width:600px;}
.grid th,.grid td {border:1px solid #C0C0C0;padding:5px;}
.head {background-color:#E8E8E8;font-weight: bold;color:#FFF;}
.alt {background-color:#E8E8E8;color:#000;}
</style>
</head>
<body>
<h1>Movies</h1>
<div>
@grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("Title"),
grid.Column("Genre"),
grid.Column("Year")
)
)
</div>
</body>
</html>