zoukankan      html  css  js  c++  java
  • GridView多列排序及排序方向图标

    网上找到的一个控件,有一点小问题,排序点击几次图标会消失,再点击一次才会出现。来源不可考证,如果作者看到请联系我,我再添加来源信息。

    public class WebGridView : GridView
        {
            
    /**/
            
    /// 
            
    /// 是否启用或者禁止多列排序
            
    /// 
            [
            Description(
    "是否启用多列排序功能"),
            Category(
    "排序"),
            DefaultValue(
    "false"),
            ]
            
    public bool AllowMultiColumnSorting
            {
                
    get
                {
                    
    object o = ViewState["EnableMultiColumnSorting"];
                    
    return (o != null ? (bool)o : false);
                }
                
    set
                {
                    AllowSorting 
    = true;
                    ViewState[
    "EnableMultiColumnSorting"= value;
                }
            }
            
    /**/
            
    /// 
            
    /// 升序时显示图标
            
    /// 
            [
            Description(
    "升序时显示图标"),
            Category(
    "排序"),
            Editor(
    "System.Web.UI.Design.UrlEditor"typeof(System.Drawing.Design.UITypeEditor)),
            DefaultValue(
    ""),

            ]
            
    public string SortAscImageUrl
            {
                
    get
                {
                    
    object o = ViewState["SortImageAsc"];
                    
    return (o != null ? o.ToString() : "");
                }
                
    set
                {
                    ViewState[
    "SortImageAsc"= value;
                }
            }
            
    /**/
            
    /// 
            
    /// 降序时显示图标
            
    /// 
            [
             Description(
    "降序时显示图标"),
             Category(
    "排序"),
             Editor(
    "System.Web.UI.Design.UrlEditor"typeof(System.Drawing.Design.UITypeEditor)),
             DefaultValue(
    ""),
             ]
            
    public string SortDescImageUrl
            {
                
    get
                {
                    
    object o = ViewState["SortImageDesc"];
                    
    return (o != null ? o.ToString() : "");
                }
                
    set
                {
                    ViewState[
    "SortImageDesc"= value;
                }
            }
            
    protected override void OnSorting(GridViewSortEventArgs e)
            {
                
    if (AllowMultiColumnSorting)
                {
                    e.SortExpression 
    = GetSortExpression(e);
                }

                
    base.OnSorting(e);
            }
            
    protected override void OnRowCreated(GridViewRowEventArgs e)
            {
                
    if (e.Row.RowType == DataControlRowType.Header)
                {
                    
    if (SortExpression != String.Empty)
                    {
                        DisplaySortOrderImages(SortExpression, e.Row);
                        
    this.CreateRow(00, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
                    }
                }
                
    base.OnRowCreated(e);
            }
            
    /**/
            
    /// 
            
    ///  获取排序表达式
            
    /// 
            protected string GetSortExpression(GridViewSortEventArgs e)
            {
                
    string[] sortColumns = null;
                
    string sortAttribute = SortExpression;

                
    if (sortAttribute != String.Empty)
                {
                    sortColumns 
    = sortAttribute.Split(",".ToCharArray());
                }
                
    if (sortAttribute.IndexOf(e.SortExpression) > 0 || sortAttribute.StartsWith(e.SortExpression))
                {
                    sortAttribute 
    = ModifySortExpression(sortColumns, e.SortExpression);
                }
                
    else
                {
                    sortAttribute 
    += String.Concat(",", e.SortExpression, " ASC ");
                }
                
    return sortAttribute.TrimStart(",".ToCharArray()).TrimEnd(",".ToCharArray());

            }
            
    /**/
            
    /// 
            
    ///  修改排序顺序
            
    /// 
            protected string ModifySortExpression(string[] sortColumns, string sortExpression)
            {
                
    string ascSortExpression = String.Concat(sortExpression, " ASC ");
                
    string descSortExpression = String.Concat(sortExpression, " DESC ");

                
    for (int i = 0; i < sortColumns.Length; i++)
                {

                    
    if (ascSortExpression.Equals(sortColumns[i]))
                    {
                        sortColumns[i] 
    = descSortExpression;
                    }

                    
    else if (descSortExpression.Equals(sortColumns[i]))
                    {
                        Array.Clear(sortColumns, i, 
    1);
                    }
                }

                
    return String.Join(",", sortColumns).Replace(",,"",").TrimStart(",".ToCharArray());

            }
            
    /**/
            
    /// 
            
    ///  获取当前的表达式对所选列进行排序
            
    /// 
            protected void SearchSortExpression(string[] sortColumns, string sortColumn, out string sortOrder, out int sortOrderNo)
            {
                sortOrder 
    = "";
                sortOrderNo 
    = -1;
                
    for (int i = 0; i < sortColumns.Length; i++)
                {
                    
    if (sortColumns[i].StartsWith(sortColumn))
                    {
                        sortOrderNo 
    = i + 1;
                        
    if (AllowMultiColumnSorting)
                        {
                            sortOrder 
    = sortColumns[i].Substring(sortColumn.Length).Trim();
                        }
                        
    else
                        {
                            sortOrder 
    = ((SortDirection == SortDirection.Ascending) ? "ASC" : "DESC");
                        }
                    }
                }
            }
            
    /**/
            
    /// 
            
    ///  绘制升序降序的图片
            
    /// 
            protected void DisplaySortOrderImages(string sortExpression, GridViewRow dgItem)
            {
                
    string[] sortColumns = sortExpression.Split(",".ToCharArray());

                
    for (int i = 0; i < dgItem.Cells.Count; i++)
                {
                    
    if (dgItem.Cells[i].Controls.Count > 0 && dgItem.Cells[i].Controls[0is LinkButton)
                    {
                        
    string sortOrder;
                        
    int sortOrderNo;
                        
    string column = ((LinkButton)dgItem.Cells[i].Controls[0]).CommandArgument;
                        SearchSortExpression(sortColumns, column, 
    out sortOrder, out sortOrderNo);
                        
    if (sortOrderNo > 0)
                        {
                            
    string sortImgLoc = (sortOrder.Equals("ASC"? SortAscImageUrl : SortDescImageUrl);

                            
    if (sortImgLoc != String.Empty)
                            {
                                Image imgSortDirection 
    = new Image();
                                imgSortDirection.ImageUrl 
    = sortImgLoc;
                                dgItem.Cells[i].Controls.Add(imgSortDirection);

                            }
                            
    else
                            {

                                
    if (AllowMultiColumnSorting)
                                {
                                    Literal litSortSeq 
    = new Literal();
                                    litSortSeq.Text 
    = sortOrderNo.ToString();
                                    dgItem.Cells[i].Controls.Add(litSortSeq);

                                }
                            }
                        }
                    }
                }

            }
        }
  • 相关阅读:
    android SD卡文件的读写
    Android Junit 单元测试步骤
    android xml解析之SAX
    android赚钱 植入有米广告方法
    android广告平台介绍
    读、写其他应用的SharedPreferences
    SQL数据基本操作
    res/raw下的资源文件读写
    容易吗?
    写给实习生的邮件
  • 原文地址:https://www.cnblogs.com/SharpDeveloper/p/2073906.html
Copyright © 2011-2022 走看看