本文介绍了在更改网格的页面索引时进行排序的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个gridview,它使用文件夹进行绑定,它具有分页和排序功能。
每次加载页面时,每页只能获得8条记录。所以每当下一页按钮事件触发时。网格将填充该文件夹中的后8条记录。
因为它有排序所以我遇到了问题。网格对所有网格记录进行排序,并希望它只对当前页面中的记录进行排序。
那么我该怎么办呢?
请帮帮我
我正在使用此代码
I have a gridview which get binds using folder it has paging and sorting.
Each time page load it only get 8 records per page. So whenever a next page button event fire. The grid get populated with next 8 records from the folder.
Since it has a sorting so I am facing a problem. The grid get sorts all record of grid and want that it only sorts the records with in the current page.
So what should I do so my sort...
Please help me
I am using this code
readonly Image _sortImage = new Image();
public string SortDireaction
{
get {
return ViewState["SortDireaction"] != null ? ViewState["SortDireaction"].ToString() : string.Empty;
}
set
{
ViewState["SortDireaction"] = value;
}
}
private string _sortDirection;
protected new void Page_Load(object sender, EventArgs e)
{
BindGrid();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
BindGrid();
}
protected void BindGrid()
{
var fl = GetList();
var dt = EntitySetToDataTable.ConvertEntitySetToDataTable(fl);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void gv_Sorting(object sender, GridViewSortEventArgs e)
{
SetSortDirection(SortDireaction);
var fl = GetList();
var dt = EntitySetToDataTable.ConvertEntitySetToDataTable(fl);
dt.DefaultView.Sort = e.SortExpression + " " + _sortDirection;
GridView1.DataSource = dt;
GridView1.DataBind();
SortDireaction = _sortDirection;
var columnIndex = 0;
foreach (var headerCell in GridView1.HeaderRow.Cells.Cast<DataControlFieldHeaderCell>().Where(headerCell => headerCell.ContainingField.SortExpression == e.SortExpression))
{
columnIndex = GridView1.HeaderRow.Cells.GetCellIndex(headerCell);
}
GridView1.HeaderRow.Cells[columnIndex].Controls.Add(_sortImage);
}
protected void SetSortDirection(string sortDirection)
{
if (sortDirection == "ASC")
{
_sortDirection = "DESC";
_sortImage.ImageUrl = "../Images/desc.png";
}
else
{
_sortDirection = "ASC";
_sortImage.ImageUrl = "../Images/asc.png";
}
}
private IEnumerable<FileList> GetList()
{
var filePaths = Directory.GetFiles(MainFolder + ddlMainFolder.SelectedValue + "/" + ddlSubFolder.SelectedValue);
return (from filePath in filePaths
let fl = new FileInfo(filePath)
select new FileList
{
FileName = Path.GetFileName(filePath), Download = filePath, FileSize = GetFileLength(fl.Length), FileSizeInBytes = fl.Length, UploadDate = string.Format("{0:g}", fl.CreationTime)
}).ToList();
}
推荐答案
这篇关于在更改网格的页面索引时进行排序的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!