上接扩展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,如需转载请自行联系原作者




相关文章
|
6月前
用dragstart、drag、dragend、dragover、drop、dragleave实现针对表格列的顺序进行拖拽排序(附带实现选择某几列数据显示或隐藏)
用dragstart、drag、dragend、dragover、drop、dragleave实现针对表格列的顺序进行拖拽排序(附带实现选择某几列数据显示或隐藏)
|
1天前
多列组合作为判断依据的具体方法是什么?
通过以上方法,可以根据具体的业务需求和数据特点,灵活地选择以多列组合作为判断数据重复值的依据,从而更准确地处理和分析数据。
14 6
|
4月前
|
开发框架 前端开发 JavaScript
使用DevExpress的GridControl实现多层级或无穷级的嵌套列表展示
使用DevExpress的GridControl实现多层级或无穷级的嵌套列表展示