zoukankan      html  css  js  c++  java
  • gridview自定义排序

    效果如图:

    首先允许排序:AllowSorting="True";开启gridview的排序事件onsorting="GridView1_Sorting",也可以双击sorting事件;其次是设置nrowdatabound="GridView1_RowDataBound",也可以双击GridView1_RowDataBound

    关联排序表达式SortExpression="字段名称"

    代码明细:

    前台代码:

    View Code
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>无标题页</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    CellPadding
    ="4" ForeColor="#333333" Width="800px"
    AllowSorting
    ="True"
    onrowdatabound
    ="GridView1_RowDataBound" onsorting="GridView1_Sorting"
    PageSize
    ="15">
    <PagerSettings Mode="NumericFirstLast"/>
    <RowStyle BackColor="#EFF3FB"/>
    <Columns>
    <asp:BoundField DataField="CourseResID" HeaderText="课程ID"/>
    <asp:BoundField DataField="CourseName" HeaderText="课程名" SortExpression="CourseName">
    <ItemStyle Width="100px"/>
    </asp:BoundField>
    <asp:BoundField DataField="upTime" HeaderText="更新时间"
    DataFormatString
    ="{0:yyyy-MM-dd}" SortExpression="upTime">
    <ItemStyle Width="160px" HorizontalAlign="Right"/>
    </asp:BoundField>
    <asp:BoundField DataField="UserName" HeaderText="用户名" SortExpression="UserName">
    <ItemStyle Width="80px" ForeColor="#33CC33"/>
    </asp:BoundField>
    </Columns>
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"/>
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Right"/>
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333"/>
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"/>
    <EditRowStyle BackColor="#2461BF"/>
    <AlternatingRowStyle BackColor="White"/>
    </asp:GridView>
    </div>
    </form>
    </body>
    </html>

    数据读取

    View Code
    ///<summary>
    /// 获取绑定数据
    ///</summary>
    ///<param name="orderFields">排序信息</param>
    ///<returns>返回表</returns>
    public DataTable GetContents(string orderFields)
    {
    string cmdTxt ="Select top 30 * From CourseRes";
    //判断排序字段是否为空,如果不为空则将排序信息加在Sql语句后面
    if (!string.IsNullOrEmpty(orderFields))
    {
    cmdTxt
    +=" Order By "+ orderFields;
    }
    SqlConnection conn
    =new SqlConnection(@"Data Source=HAPPY-WITBANK;Initial Catalog=le2011;Persist Security Info=True;User ID=sa;Password=happy");
    SqlCommand comm
    =new SqlCommand(cmdTxt, conn);
    SqlDataAdapter adap
    =new SqlDataAdapter(comm);
    DataTable dt
    =new DataTable();
    adap.Fill(dt);

    return dt;
    }

    排序信息代码,可以通用;注,要用时只需将上面的绑定Sql数据修改即可

    View Code
    publicpartialclass 自定义分页 : System.Web.UI.Page
    {
    protectedvoid Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    DataLoad();
    }
    }
    ///<summary>
    /// 数据加载
    ///</summary>
    privatevoid DataLoad()
    {
    string sortedField ="";
    //获取排序字段的信息
    if (ViewState["SortedField"] !=null)
    {
    //如果存在排序信息,则将排序信息转换为字典存储
    Dictionary<string, string> sorted = (Dictionary<string, string>)ViewState["SortedField"];
    //便利取值
    foreach (KeyValuePair<string, string> kvp in sorted)
    {
    //key排序字段,value排序信息
    sortedField = kvp.Key +""+ kvp.Value;
    }
    }

    //数据绑定
    this.GridView1.DataSource = GetContents(sortedField);
    this.GridView1.DataBind();
    }

    protectedvoid GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    #region//加排序的头方法
    //判断是否是表头
    if (e.Row.RowType == DataControlRowType.Header)
    {
    //判断是否进行排序
    if (ViewState["SortedField"] !=null)
    {
    //恢复排序信息
    Dictionary<string, string> order = (Dictionary<string, string>)ViewState["SortedField"];
    //循环表头的单元格,判断是否有需要排序的单元格
    for (int i =0; i < e.Row.Cells.Count; i++)
    {
    if (e.Row.Cells[i].Controls.Count >0)
    {
    //是否是排序字段信息
    LinkButton lb = e.Row.Cells[i].Controls[0] as LinkButton;
    if (lb !=null)
    {
    //反序和降序
    if (order.ContainsKey(lb.CommandArgument))
    {
    Literal li
    =new Literal();
    if (order[lb.CommandArgument] =="ASC")
    {
    li.Text
    ="";
    }
    else
    {
    li.Text
    ="";
    }
    //
    e.Row.Cells[i].Controls.Add(li);
    }
    }
    }
    }
    }
    }
    #endregion 完成排序

    }

    protectedvoid GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
    //
    Dictionary<string, string> sorted =new Dictionary<string, string>();
    //从ViewState读取上一次的排序信息。
    if (ViewState["SortedField"] ==null)
    {
    //如果没有排序信息,则将当前点击的对象(即点击的排序字段)和排序信息添加到字典中
    sorted.Add(e.SortExpression, "ASC");
    //将排序信息放在字典中保存
    ViewState["SortedField"] = sorted;
    }
    else
    {
    //判断是否点击相同的排序,如果是相同的排序,
    //就是用反序
    //如果不是想通的排序,那就新的派讯,是用升序
    sorted = (Dictionary<string, string>)ViewState["SortedField"];
    if (sorted.ContainsKey(e.SortExpression))
    {
    if (sorted[e.SortExpression] =="ASC")
    {
    sorted[e.SortExpression]
    ="DESC";
    }
    else
    {
    sorted[e.SortExpression]
    ="ASC";
    }
    }
    else
    {
    sorted.Clear();
    sorted.Add(e.SortExpression,
    "ASC");
    ViewState[
    "SortedField"] = sorted;
    }
    }

    //重新加载数据
    DataLoad();

    }
    }
  • 相关阅读:
    对一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。
    Java基础学习笔记四 Java基础语法
    Java基础学习笔记一 Java介绍
    Java基础学习笔记二 Java基础语法
    Elasticsearch重要配置
    Elasticsearch配置
    Elasticsearch安装详解
    Elasticsearch文档查询
    Elasticsearch索引和文档操作
    Angular4项目,默认的package.json创建及配置
  • 原文地址:https://www.cnblogs.com/happygx/p/2013417.html
Copyright © 2011-2022 走看看