This is best solution I found :) hope it can help you:
SqlConnection con = new SqlConnection(CS);
SqlCommand cmd = new SqlCommand("select * from candidate", con);
DataTable dt = new DataTable();
SqlDataAdapter a = new SqlDataAdapter(cmd)
a.Fill(dt);
var result = new List<dynamic>();
foreach (DataRow row in dt.Rows)
{
var obj = (IDictionary<string, object>)new ExpandoObject();
foreach (DataColumn col in dt.Columns)
{
obj.Add(col.ColumnName, row[col.ColumnName]);
}
result.Add(obj);
}
WebGrid grid = new WebGrid(Model, canPage: true, rowsPerPage: 15);
Then in view you can use:
@grid.GetHtml(htmlAttributes: new { id = "empTable" },
tableStyle: "table table-striped table-hover",
headerStyle: "header",
alternatingRowStyle: "alt",
selectedRowStyle: "select",
columns: grid.Columns(
grid.Column("col1name", "Column title"),
grid.Column("col2name", "Column2 title")
))
where grid is your WebGrid grid
variable.