当分页被禁用时,我试图基于单击标题对GridView进行排序。
如何检索网格的内容?
GridView.Rows仅在选定页面上为我提供行。
我不想从数据库中进行其他选择,而是要订购它。而且,也不想将其保存在SessionState中。
我不知道什么是“默认” SortExpression的样子。
并且此SortExpression将按ID对DESC或ASC进行排序,但是如何进行一些自定义排序呢?还是按其他类型排序?
如何按ValueFromIncluded排序
//example
<asp:GridView runat="server"
ID="GridID"
SelectMethod="GridView_GetData"
ItemType="ItemType"
AllowPaging="true">
<Columns>
<asp:TemplateField HeaderText="Id" SortExpression="Id">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Item?.Id%>'/>
</ItemTemplate>
</asp:TemplateField>
asp:TemplateField HeaderText="ValueFromIncluded" SortExpression="ValueFromIncluded">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Item?.ValueFromIncluded.SomeOtherId%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
// SelectMethod.
public IQueryable<ItemType> GridView_GetData()
{
IQueryable<ItemType> q = dbx.ItemType
.Include(f => f.OtherType);
}
因此,您需要做的是制作另一个类并在Grid中使用它。创建新的类ItemTypeForGrid,然后在aspx页面上ItemType =“ ItemType + ItemTypeForGrid”
public class ItemTypeForGrid: ItemType
{
public string ValueFromIncluded{ get; internal set; }
}
所以代码看起来像
<asp:GridView runat="server"
ID="GridID"
SelectMethod="GridView_GetData"
ItemType="ItemType+ItemTypeForGrid"
AllowPaging="true">
<Columns>
<asp:TemplateField HeaderText="Id" SortExpression="Id">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Item?.Id%>'/>
</ItemTemplate>
</asp:TemplateField>
asp:TemplateField HeaderText="ValueFromIncluded" SortExpression="ValueFromIncluded">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Item?.ValueFromIncluded.SomeOtherId%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
// SelectMethod。
public IQueryable<ItemTypeForGrid> GridView_GetData()
{
IQueryable<ItemType> q = context.ItemType
.Include(f => f.OtherType);
// AND HERE SELECT WHAT YOU NEED IN GRID
var qForGrid = q.Select( s=> new ItemTypeForGrid(){
ValueFromIncluded = r.ItemType.OtherType });
}
这样您可以排序!但更重要的是,它只选择需要在GridView中看到的内容,而不选择所有包含的表中的所有属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句