上接扩展GridView控件(2) - 复合排序和排序状态提示

简介:
/*测试版的实现 开始*/
介绍
在用GridView自带的排序功能排序时,无法直观的知道当前是通过哪个字段排序?是升序还是降序?所以扩展一下,用图片或文字的形式来提示一下当前是根据哪个字段排序,是升序还是降序。


控件开发
1、新建一个继承自GridView的类。
/// <summary> 
         /// 继承自GridView 
         /// </summary> 
        [ 
        ToolboxData( @"<{0}:SmartGridView runat='server'></{0}:SmartGridView>"), 
        ParseChildren( true), 
        PersistChildren( false
        ] 
         public  class SmartGridView : GridView 
        { 
        }
 
2、新建一个SortTip实体类,有4个属性,分别是降序提示图片、升序提示图片、降序提示文本和升序提示文本
using System; 
using System.Collections.Generic; 
using System.Text; 
 
using System.ComponentModel; 
 
namespace YYControls.SmartGridView 

         /// <summary> 
         /// 排序提示类 
         /// </summary> 
        [TypeConverter( typeof(ExpandableObjectConverter))] 
         public  class SortTip 
        { 
                 private  string _sortDescImage; 
                 /// <summary> 
                 /// 降序提示图片 
                 /// </summary> 
                [ 
                Description( "降序提示图片"), 
                Category( "扩展"), 
                Editor( "System.Web.UI.Design.UrlEditor"typeof(System.Drawing.Design.UITypeEditor)), 
                DefaultValue(""), 
                NotifyParentProperty( true
                ] 
                 public  string SortDescImage 
                { 
                        get {  return _sortDescImage; } 
                        set { _sortDescImage = value; } 
                } 
 
                 private  string _sortAscImage; 
                 /// <summary> 
                 /// 升序提示图片 
                 /// </summary> 
                [ 
                Description( "升序提示图片"), 
                Category( "扩展"), 
                Editor( "System.Web.UI.Design.UrlEditor"typeof(System.Drawing.Design.UITypeEditor)), 
                DefaultValue(""), 
                NotifyParentProperty( true
                ] 
                 public  string SortAscImage 
                { 
                        get {  return _sortAscImage; } 
                        set { _sortAscImage = value; } 
                } 
 
                 private  string _sortDescText; 
                 /// <summary> 
                 /// 降序提示文本 
                 /// </summary> 
                [ 
                Description( "降序提示文本"), 
                Category( "扩展"), 
                DefaultValue(""), 
                NotifyParentProperty( true
                ] 
                 public  string SortDescText 
                { 
                        get {  return _sortDescText; } 
                        set { _sortDescText = value; } 
                } 
 
                 private  string _sortAscText; 
                 /// <summary> 
                 /// 升序提示文本 
                 /// </summary> 
                [ 
                Description( "升序提示文本"), 
                Category( "扩展"), 
                DefaultValue(""), 
                NotifyParentProperty( true
                ] 
                 public  string SortAscText 
                { 
                        get {  return _sortAscText; } 
                        set { _sortAscText = value; } 
                } 
 
                 /// <summary> 
                 /// ToString() 
                 /// </summary> 
                 /// <returns></returns> 
                 public  override  string ToString() 
                { 
                         return  "SortTip"
                } 
        } 
}
 
3、在继承自GridView的那个类中加1个复杂对象属性,这个复杂对象就是第2步创建的那个SortTip
private SortTip _sortTip; 
                 /// <summary> 
                 /// 排序提示信息 
                 /// </summary> 
                [ 
                Description( "排序提示信息"), 
                Category( "扩展"), 
                DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
                PersistenceMode(PersistenceMode.InnerProperty) 
                ] 
                 public  virtual SortTip SortTip 
                { 
                        get 
                        { 
                                 if (_sortTip ==  null
                                { 
                                        _sortTip =  new SortTip(); 
                                } 
                                 return _sortTip; 
                        } 
                }
 
4、重写OnRowDataBound实现给字段标题加上排序状态的功能。主要是根据GridView.SortExpression和GridView.SortDirection来实现这样的功能。
/// <summary> 
                 /// OnRowDataBound 
                 /// </summary> 
                 /// <param name="e"></param> 
                 protected  override  void OnRowDataBound(GridViewRowEventArgs e) 
                { 
                         if (e.Row.RowType == DataControlRowType.Header) 
                        { 
                                 // GridViewRow的每个TableCell 
                                 for ( int i = 0; i < e.Row.Cells.Count; i++) 
                                { 
                                         // TableCell里有一个Control并且是LinkButton 
                                         if (e.Row.Cells[i].Controls.Count == 1 && e.Row.Cells[i].Controls[0]  is LinkButton) 
                                        { 
                                                 // LinkButton的命令参数等于排序字段 
                                                 if (((LinkButton)e.Row.Cells[i].Controls[0]).CommandArgument ==  this.SortExpression) 
                                                { 
                                                        Image img =  null
                                                        Label lbl =  null
 
                                                         // 升序 
                                                         if ( this.SortDirection == SortDirection.Ascending) 
                                                        { 
                                                                 // 升序图片 
                                                                 if (!String.IsNullOrEmpty(_sortTip.SortAscImage)) 
                                                                { 
                                                                        img =  new Image(); 
                                                                        img.ImageUrl =  base.ResolveUrl(_sortTip.SortAscImage); 
                                                                } 
                                                                 // 升序文字 
                                                                 if (!String.IsNullOrEmpty(_sortTip.SortAscText)) 
                                                                { 
                                                                        lbl =  new Label(); 
                                                                        lbl.Text = _sortTip.SortAscText; 
                                                                } 
                                                        } 
                                                         // 降序 
                                                         else  if ( this.SortDirection == SortDirection.Descending) 
                                                        { 
                                                                 // 降序图片 
                                                                 if (!String.IsNullOrEmpty(_sortTip.SortDescImage)) 
                                                                { 
                                                                        img =  new Image(); 
                                                                        img.ImageUrl =  base.ResolveUrl(_sortTip.SortDescImage); 
                                                                } 
                                                                 // 降序文字 
                                                                 if (!String.IsNullOrEmpty(_sortTip.SortDescText)) 
                                                                { 
                                                                        lbl =  new Label(); 
                                                                        lbl.Text = _sortTip.SortDescText; 
                                                                } 
                                                        } 
 
                                                         // TableCell里加上图片 
                                                         if (img !=  null
                                                        { 
                                                                e.Row.Cells[i].Controls.Add(img); 
                                                        } 
                                                         // TableCell里加上文字 
                                                         if (lbl !=  null
                                                        { 
                                                                e.Row.Cells[i].Controls.Add(lbl); 
                                                        } 
                                                } 
                                        } 
                                } 
                        } 
 
                         base.OnRowDataBound(e); 
                }
 
控件使用
添加这个控件到工具箱里,然后拖拽到webform上,设置其SortTip下的4个属性即可。SortAscImage是升序提示图片;SortAscText是升序提示文本;SortDescImage是降序提示图片;SortDescText是降序提示文本
ObjData.cs
控件使用 
添加这个控件到工具箱里,然后拖拽到webform上,设置其SortTip下的4个属性即可。SortAscImage是升序提示图片;SortAscText是升序提示文本;SortDescImage是降序提示图片;SortDescText是降序提示文本 
ObjData.cs 
using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
 
using System.ComponentModel; 
 
/// <summary> 
/// OjbData 的摘要说明 
/// </summary> 
public  class OjbData 

         public OjbData() 
        { 
                 // 
                 // TODO: 在此处添加构造函数逻辑 
                 // 
        } 
 
        [DataObjectMethod(DataObjectMethodType.Select,  true)] 
         public DataTable Select() 
        { 
                DataTable dt =  new DataTable(); 
                dt.Columns.Add( "no"typeof( string)); 
                dt.Columns.Add( "name"typeof( string)); 
 
                 for ( int i = 0; i < 30; i++) 
                { 
                        DataRow dr = dt.NewRow(); 
                        dr[0] =  "no" + i.ToString().PadLeft(2, '0'); 
                        dr[1] =  "name" + i.ToString().PadLeft(2, '0'); 
 
                        dt.Rows.Add(dr); 
                } 
 
                 return dt; 
        } 
}
 
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
        <title>无标题页</title> 
</head> 
<body> 
        <form id="form1" runat="server"> 
                <div> 
                        <yyc:SmartGridView ID="SmartGridView1" runat="server" AutoGenerateColumns="True" 
                                AllowSorting="True" DataSourceID="ObjectDataSource1"> 
                                <Columns> 
                                        <asp:ButtonField CommandName="ConfirmTest" Text="确认按钮测试" /> 
                                </Columns> 
                                <SortTip SortAscImage="~/Images/asc.gif" SortDescImage="~/Images/desc.gif" /> 
                        </yyc:SmartGridView> 
                        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select" 
                                TypeName="OjbData"></asp:ObjectDataSource> 
                </div> 
    </form> 
</body> 
</html>
 
/*测试版的实现 结束*/
 


     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/345445,如需转载请自行联系原作者




相关文章
|
7月前
用dragstart、drag、dragend、dragover、drop、dragleave实现针对表格列的顺序进行拖拽排序(附带实现选择某几列数据显示或隐藏)
用dragstart、drag、dragend、dragover、drop、dragleave实现针对表格列的顺序进行拖拽排序(附带实现选择某几列数据显示或隐藏)
|
4月前
|
JavaScript 前端开发 开发者
"震撼揭秘!ElementUI Table组件大变身,一键自定义升序降序按钮,排序操作秒变炫酷舞台,点击即燃,你的列表排序从此不再平凡!"
【8月更文挑战第16天】在Vue.js开发中,ElementUI的Table组件因其实用性和灵活性深受喜爱。本文通过一个后台管理系统用户列表的案例,展示了如何自定义添加“升序”和“降序”按钮以增强排序功能。首先确保安装Vue与ElementUI;接着定义表格数据及初始排序状态;利用scoped slot自定义列模板,加入按钮并绑定点击事件;最后实现在点击按钮时更新排序状态并重新排序数据的逻辑。这种方法不仅改善了用户体验,还为开发者提供了灵活调整排序逻辑的空间。
166 3
|
1月前
多列组合作为判断依据的具体方法是什么?
通过以上方法,可以根据具体的业务需求和数据特点,灵活地选择以多列组合作为判断数据重复值的依据,从而更准确地处理和分析数据。
38 6
MPAndroidChart_并列柱状图,及如何实现点击隐藏掉不需要的条目。
在比赛中遇到了MP的并列柱状图,看网上对这方面记录并不是很多,所以今天就做一个教程吧。
208 0
MPAndroidChart_并列柱状图,及如何实现点击隐藏掉不需要的条目。
|
PHP
PHP数组排序 解决数值型版本号排序错乱
PHP数组排序 解决数值型版本号排序错乱
137 0
|
小程序 JavaScript API
小程序实现索引列表排序
小程序实现索引列表排序
405 0
HorizontalScrollView包裹RecyclerView,使用StaggeredGridLayoutManager均分网格形成表格状列表,不固定列,每次刷新数据列位置异常错乱变动问题
HorizontalScrollView包裹RecyclerView,使用StaggeredGridLayoutManager均分网格形成表格状列表,不固定列,每次刷新数据列位置异常错乱变动问题 问题描述:用Horizon...
2029 0

热门文章

最新文章