MVC 3 WebGrid Column Sort alphabatically, not according to table structure

送分小仙女□ 提交于 2019-12-24 01:08:55

问题


I have assigned data to the webgrid but it sorting the columns alphabetically. it is not showing columns as like in asp datagrid according to table structure.

for example if table have columns as"

id  title  artid   

then grid showing them after sorting the columns as:

artid  id title

回答1:


If you are still looking for this answer I have found it here:

 <h2>Index</h2>
@{
    var grid = new WebGrid(source: Model,
        defaultSort: "id",
        rowsPerPage:10);    
}
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<div id="grid">
    @grid.GetHtml(columns: grid.Columns(
                    grid.Column("id", "ID"),
                    grid.Column("title", "Title"),
                    grid.Column("artist", "Artist")));
</div>

Hope this helps!




回答2:


In addition to stuartmclark's post above, if you want to sort by descending, you can set the sort direction after your grid initialization.

grid.SortDirection = SortDirection.Descending;


来源:https://stackoverflow.com/questions/6149964/mvc-3-webgrid-column-sort-alphabatically-not-according-to-table-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!