首页 > 其他分享 >Gridview排序与分页-不使用“DataSourceControl DataSource”的情况下如何分页和排序 ...

Gridview排序与分页-不使用“DataSourceControl DataSource”的情况下如何分页和排序 ...

时间:2022-12-23 10:03:45浏览次数:37  
标签:... GridViewSortDirection return 分页 gvInvoiceHistory new 排序 public string


如果你在GridView控件上设置 AllowPaging="true" or AllowSorting="true" 而没有使用使用数据源控件 DataSource (i.e. SqlDataSource, ObjectDataSource),运行则会出现下列错误:
当你在GridView控件上单击下一页时:
The GridView 'GridViewID' fired event PageIndexChanging which wasn't handled.
当你点击排序时,则回出现:
The GridView 'GridViewID' fired event Sorting which wasn't handled.

不使用“DataSourceControl DataSource”的情况下如何分页和排序 ...

你必须添加一个操作才可以排序及分页。。

public class WebHandler
{
private WebHandler() { }
public static readonly WebHandler Instance = new WebHandler();

#region GridView Handler

public string GridViewSortExpression
{
get { return HttpContext.Current.Session["SortExpression"] as string ?? string.Empty; }
set { HttpContext.Current.Session["SortExpression"] = value; }
}

public string GridViewSortDirection
{
get { return HttpContext.Current.Session["SortDirection"] as string ?? "ASC"; }
set { HttpContext.Current.Session["SortDirection"] = value; }
}

public string GetSortDirection()
{
switch (GridViewSortDirection)
{
case "ASC":
GridViewSortDirection = "DESC";
break;
case "DESC":
GridViewSortDirection = "ASC";
break;
}
return GridViewSortDirection;
}

public DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)
{
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
if (GridViewSortExpression != string.Empty)
{
if (isPageIndexChanging)
{
dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection);
}
else
{
dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GetSortDirection());
}
}
return dataView;
}
else
{
return new DataView();
}
}

#endregion
}

aspx & aspx.cs

<asp:GridView ID="gvInvoiceHistory" runat="server" AllowPaging="True"  OnPageIndexChanging="gvInvoiceHistory_PageIndexChanging" OnRowCreated="gvInvoices_RowCreated" OnSorting="gvInvoiceHistory_Sorting" AllowSorting="True" AutoGenerateColumns="False"  >


//aspx.cs
private readonly WebHandler webHandler = WebHandler.Instance;

protected void gvInvoiceHistory_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvInvoiceHistory.DataSource = webHandler.SortDataTable(GetInvoiceList(), true);
gvInvoiceHistory.PageIndex = e.NewPageIndex;
gvInvoiceHistory.DataBind();
}


protected void gvInvoiceHistory_Sorting(object sender, GridViewSortEventArgs e)
{
webHandler.GridViewSortExpression = e.SortExpression;
int pageIndex = gvInvoiceHistory.PageIndex;
gvInvoiceHistory.DataSource = webHandler.SortDataTable(GetInvoiceList(), false);
gvInvoiceHistory.DataBind();
gvInvoiceHistory.PageIndex = pageIndex;
}


public virtual DataTable GetInvoiceList()
{
DataTable result = new DataTable();
...........
return result;
}

 

//DataBind 排序图标丢失,

 

protected virtual void gvInvoices_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row != null && e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell cell in e.Row.Cells)
{
if (cell.HasControls())
{
LinkButton button = cell.Controls[0] as LinkButton;
if (button != null)
{
//Label lblsort = new Label();
if (ddlParentInvoice.SelectedValue != "-1" && webHandler.GridViewSortExpression == button.CommandArgument)
{
Image image = new Image();
if (webHandler.GetSortDirection() == "ASC")
{
image.SkinID = "SortArrowDown";
//lblsort.Text = " <font>▼</font>";
}
else
{
image.SkinID = "SortArrowUp";
//lblsort.Text = " <font>▲</font>";
}
cell.Controls.Add(image);
}
}
}
}
}
}

 

 

 



标签:...,GridViewSortDirection,return,分页,gvInvoiceHistory,new,排序,public,string
From: https://blog.51cto.com/u_15116285/5964877

相关文章

  • 八大排序算法
    1.冒泡排序特点:相邻元素两两比较,把值大的元素往下交换。缺点:冒泡排序的时间复杂度太高冒泡排序的过程如下图所示,其展示了一趟排序的过程,在下一趟的排序过程中,最后一个排......
  • Bash Shell自定义助手函数git-submodule-foreach:遍历对每个子模块仓库执行自定义的函
    BashShell自定义助手函数git-submodule-foreach:遍历对每个子模块仓库执行自定义的函数或命令序列...概述:在一个大型项目下,我们通常通过GitSubmodule(子模块)机制引入了其......
  • VUE html2canvas+jsPdf 下载PDF文件并且以文件流的形式传给后端 + 分页处理截断文字问
    安装:npminstall html2canvas--savenpminstalljsPDF--save配置:  //转化PDF组件importhtmlToPdffrom'@/utils/contract/htmlToPdf'Vue.use(htmlToPdf)......
  • 分页
     1.利用el分页组件  2.定义分页组件绑定的数据模型  3.异步,获取分页数据  4.回显  查询 ......
  • 深度分页问题分析和优化
    数据集test_db下载:https://gitee.com/lzjcnb/test_db数据集安装:mysql-uroot-p-t<employees.sql深度分页问题后端开发为了防止一次加载太多数据到内存,对内存占用和......
  • react中的api获取数组排序
    [javascript-SortanarrayofobjectsinReactandrenderthem-StackOverflow](https://stackoverflow.com/questions/43572436/sort-an-array-of-objects-in-reac......
  • 分布式数据库各回滚恢复工具学习(binlog2sql,MyFlash,my2sql ...)
    1、背景:DBA或开发人员,有时会误删或者误更新数据,如果是线上环境并且影响较大,就需要能快速回滚。传统恢复方法是利用备份重搭实例,再应用去除错误sql后的binlog来恢复数据。此......
  • 【算法实践】他山之石,可以攻玉--利用完全二叉树快速实现堆排序
    前言什么是堆堆是一种数据结构,它是完全二叉树或者是近似完全二叉树的一种数据结构,树中每个结点的值都不小于(或不大于)其左右孩子结点的值。何为完全二叉树完全二叉树是一种......
  • Day26.1.冒泡排序
    Day26.1.冒泡排序1.内容两层循环,外层冒泡轮数,内层依次比较,时间复杂度O(n2),{实际为(n-1)*n/2},(具体参考数据结构)相邻的数比较一轮确定一个数的位置最后一个数......
  • 堆排序
    堆排序时间复杂度:O(logn)先创建一个堆,然后调整堆,调整过程是将节点和子节点进行比较,将其中最大的值变为父节点,递归调整调整次数lgn,最后将根节点和尾节点交换再n次调整O(n......