扩展gridview的控件的实现(网转)

文章出处:http://www.diybl.com/course/1_web/webjs/200838/103890.html

控件使用

2008100120362542.png


1、鼠标经过行的时候改变该行的样式,鼠标离开行的时候恢复该行的样式
使用方法(设置属性): 
MouseOverCssClass - 鼠标经过行时行的 CSS 类名

2、对多个字段进行复合排序;升序、降序的排序状态提示
使用方法(设置SmartSorting复合属性):
AllowSortTip - 是否启用排序提示
AllowMultiSorting - 是否启用复合排序
SortAscImageUrl - 升序提示图片的URL(不设置则使用默认图片)
SortDescImageUrl - 降序提示图片的URL(不设置则使用默认图片)
SortAscText - 升序提示文本
SortDescText - 降序提示文本

3、根据按钮的CommandName设置其客户端属性
使用方法(设置ClientButtons集合属性):
BoundCommandName - 需要绑定的CommandName
AttributeKey - 属性的名称
AttributeValue - 属性的值(两个占位符:{0} - CommandArgument;{1} - Text)
Position - 属性的值的位置

4、联动复选框(复选框的全选和取消全选)。选中指定的父复选框,则设置指定的所有子复选框为选中状态;取消选中指定的父复选框,则设置指定的所有子复选框为取消选中状态
使用方法(设置CascadeCheckboxes集合属性):
ParentCheckboxID - 模板列中 父复选框ID
ChildCheckboxID - 模板列中 子复选框ID
YYControls.Helper.SmartGridView中的静态方法
List GetCheckedDataKey(GridView gv, int columnIndex)
List GetCheckedDataKey(GridView gv, string checkboxId)

5、固定指定行、指定列,根据RowType固定行,根据RowState固定行
使用方法(设置FixRowColumn复合属性):
FixRowType - 需要固定的行的RowType(用逗号“,”分隔)
FixRowState - 需要固定的行的RowState(用逗号“,”分隔)
FixRows - 需要固定的行的索引(用逗号“,”分隔)
FixColumns - 需要固定的列的索引(用逗号“,”分隔)
TableWidth - 表格的宽度
TableHeight - 表格的高度

6、响应行的单击事件和双击事件,并在服务端处理
使用方法(设置属性):
BoundRowClickCommandName - 行的单击事件需要绑定的CommandName
BoundRowDoubleClickCommandName - 行的双击事件需要绑定的CommandName

7、行的指定复选框选中的时候改变该行的样式,行的指定复选框取消选中的时候恢复该行的样式
使用方法(设置CheckedRowCssClass复合属性):
CheckBoxID - 模板列中 数据行的复选框ID
CssClass - 选中的行的 CSS 类名

8、导出数据源的数据为Excel、Word或Text(应保证数据源的类型为DataTable或DataSet)
使用方法:
为SmartGridView添加的方法
Export(string fileName)
Export(string fileName, ExportFormat exportFormat)
Export(string fileName, ExportFormat exportFormat, Encoding encoding)
Export(string fileName, int[] columnIndexList, ExportFormat exportFormat, Encoding encoding)
Export(string fileName, int[] columnIndexList, string[] headers, ExportFormat exportFormat, Encoding encoding)

9、给数据行增加右键菜单,响应服务端事件或超级链接
使用方法(设置ContextMenus集合属性):
Text - 菜单的文本内容
BoundCommandName - 需要绑定的CommandName
NavigateUrl - 链接的URL
Target - 链接的目标窗口或框架
SmartGridView的属性ContextMenuCssClass - 右键菜单的级联样式表 CSS 类名(右键菜单的结构div ul li a)

10、自定义分页样式。显示总记录数、每页记录数、当前页数、总页数、首页、上一页、下一页、末页和分页按钮
使用方法(设置CustomPagerSettings复合属性):
PagingMode - 自定义分页的显示模式
TextFormat - 自定义分页的文本显示样式(四个占位符:{0}-每页显示记录数;{1}-总记录数;{2}-当前页数;{3}-总页数)

11、合并指定列的相邻且内容相同的单元格
使用方法(设置属性):
MergeCells -  需要合并单元格的列的索引(用逗号“,”分隔)
/*正式版的实现 开始*/
介绍
扩展GridView控件:
对多个字段进行复合排序;升序、降序的排序状态提示

使用方法(设置SmartSorting复合属性):
AllowSortTip - 是否启用排序提示
AllowMultiSorting - 是否启用复合排序
SortAscImageUrl - 升序提示图片的URL(不设置则使用默认图片)
SortDescImageUrl - 降序提示图片的URL(不设置则使用默认图片)
SortAscText - 升序提示文本
SortDescText - 降序提示文本


关键代码

None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI;
None.gif
None.gif
namespace YYControls.SmartGridViewFunction

{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 扩展功能:复合排序和排序状态提示
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SmartSortingFunction : ExtendFunction
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 构造函数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public SmartSortingFunction()
InBlock.gif            : 
base()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 构造函数
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="sgv">SmartGridView对象</param>

InBlock.gif        public SmartSortingFunction(SmartGridView sgv)
InBlock.gif            : 
base(sgv)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 扩展功能的实现
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Execute()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 开启SmartGridView的排序功能
InBlock.gif            
// this._sgv.AllowSorting = true;
InBlock.gif

InBlock.gif            
this._sgv.Sorting += new System.Web.UI.WebControls.GridViewSortEventHandler(_sgv_Sorting);
InBlock.gif            
this._sgv.RowDataBoundCell += new SmartGridView.RowDataBoundCellHandler(_sgv_RowDataBoundCell);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// SmartGridView的RowDataBoundCell事件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif        
/// <param name="gvtc"></param>

InBlock.gif        void _sgv_RowDataBoundCell(object sender, GridViewTableCell gvtc)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!String.IsNullOrEmpty(this._sgv.SortExpression) 
InBlock.gif                
&& this._sgv.SmartSorting.AllowSortTip
InBlock.gif                
&& gvtc.RowType == DataControlRowType.Header)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 显示排序状态提示
InBlock.gif
                DisplaySortTip(this._sgv.SortExpression, gvtc.TableCell);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Sorting事件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif        
/// <param name="e"></param>

InBlock.gif        void _sgv_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 如果允许复合排序的话,则设置复合排序表达式
InBlock.gif
            if (this._sgv.SmartSorting.AllowMultiSorting)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                e.SortExpression 
= GetSortExpression(e);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获得复合排序表达式
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="e"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        protected string GetSortExpression(GridViewSortEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string[] sortColumns = null;
InBlock.gif            
string sortAttribute = this._sgv.SortExpression;
InBlock.gif
InBlock.gif            
if (!String.IsNullOrEmpty(sortAttribute))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sortColumns 
= sortAttribute.Split('','');
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// 如果原排序表达式有当前排序字段
InBlock.gif
            if (sortAttribute.IndexOf(e.SortExpression) != -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 更新排序表达式
InBlock.gif
                sortAttribute = ModifySortExpression(sortColumns, e.SortExpression);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 为原排序表达式添加新的排序规则(升序)
InBlock.gif
                sortAttribute += String.Concat(",", e.SortExpression, " ASC");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return sortAttribute.Trim('','');
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 更新排序表达式
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sortColumns">各个字段的排序表达式数组</param>
InBlock.gif        
/// <param name="sortExpression">当前需要排序的字段(该字段在sortColumns数组中)</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        protected string ModifySortExpression(string[] sortColumns, string sortExpression)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 当前需要排序的字段的升序表达式
InBlock.gif
            string ascSortExpression = String.Concat(sortExpression, " ASC");
InBlock.gif            
// 当前需要排序的字段的降序表达式
InBlock.gif
            string descSortExpression = String.Concat(sortExpression, " DESC");
InBlock.gif
InBlock.gif            
for (int i = 0; i < sortColumns.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 各个字段的排序表达式数组中,有当前需要排序的字段的升序表达式
InBlock.gif
                if (ascSortExpression.Equals(sortColumns[i]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// 当前排序字段由升序变为降序
InBlock.gif
                    sortColumns[i] = descSortExpression;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
// 各个字段的排序表达式数组中,有当前需要排序的字段的降序表达式
InBlock.gif
                else if (descSortExpression.Equals(sortColumns[i]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// 从各个字段的排序表达式数组中,删除当前需要排序的字段
InBlock.gif
                    Array.Clear(sortColumns, i, 1);
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return String.Join(",", sortColumns).Replace(",,"",").Trim('','');
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 显示排序状态提示
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sortExpression">排序表达式</param>
ExpandedSubBlockEnd.gif        
/// <param name="tc">Header的TableCell</param>

InBlock.gif        protected void DisplaySortTip(string sortExpression, TableCell tc)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string[] sortColumns = sortExpression.Split('','');
InBlock.gif
InBlock.gif            
if (tc.Controls.Count > 0 && tc.Controls[0is LinkButton)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string columnName = ((LinkButton)tc.Controls[0]).CommandArgument;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
int sortOrderIndex = Array.FindIndex(sortColumns, delegate(string s) dot.gifreturn s.IndexOf(columnName) != -1; });
InBlock.gif
InBlock.gif                
// 如果排序表达式中有当前列
InBlock.gif
                if (sortOrderIndex != -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// 当前列应该是升序还是降序(区分两种情况:复合排序和非复合排序)
InBlock.gif
                    SortDirection sortDirection = SortDirection.Ascending;
InBlock.gif                    
if (this._sgv.SmartSorting.AllowMultiSorting && sortColumns[sortOrderIndex].IndexOf("DESC"!= -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sortDirection 
= SortDirection.Descending;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (!this._sgv.SmartSorting.AllowMultiSorting && this._sgv.SortDirection == SortDirection.Descending)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sortDirection 
= SortDirection.Descending;
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
// 排序状态提示(图片地址)
InBlock.gif
                    string sortImageUrl = null;
InBlock.gif                    
// 排序状态提示(文本)
InBlock.gif
                    string sortText = null;
InBlock.gif                    
if (sortDirection == SortDirection.Ascending)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sortText 
= this._sgv.SmartSorting.SortAscText;
InBlock.gif
InBlock.gif                        sortImageUrl 
= this._sgv.SmartSorting.SortAscImageUrl;
InBlock.gif
InBlock.gif                        
if (String.IsNullOrEmpty(sortImageUrl))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            sortImageUrl 
= 
InBlock.gif                                
this._sgv.Page.ClientScript.GetWebResourceUrl
InBlock.gif                                (
InBlock.gif                                    
this.GetType(),
InBlock.gif                                    
"YYControls.SmartGridView.Resources.Asc.gif"
InBlock.gif                                );
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sortText 
= this._sgv.SmartSorting.SortDescText;
InBlock.gif
InBlock.gif                        sortImageUrl 
= this._sgv.SmartSorting.SortDescImageUrl;
InBlock.gif
InBlock.gif                        
if (String.IsNullOrEmpty(sortImageUrl))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            sortImageUrl 
= 
InBlock.gif                                
this._sgv.Page.ClientScript.GetWebResourceUrl
InBlock.gif                                (
InBlock.gif                                    
this.GetType(),
InBlock.gif                                    
"YYControls.SmartGridView.Resources.Desc.gif"
InBlock.gif                                );
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
// 添加排序状态提示图片
InBlock.gif
                    Image imgSortDirection = new Image();
InBlock.gif                    imgSortDirection.ImageUrl 
= sortImageUrl;
InBlock.gif                    tc.Controls.Add(imgSortDirection);
InBlock.gif
InBlock.gif                    
// 添加排序状态提示文本
InBlock.gif
                    tc.Controls.Add(new LiteralControl(sortText));
InBlock.gif
InBlock.gif                    
if (this._sgv.SmartSorting.AllowMultiSorting)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
// 添加排序状态提示序号
InBlock.gif
                        tc.Controls.Add(new LiteralControl((sortOrderIndex + 1).ToString()));
ExpandedSubBlockEnd.gif                    }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif                    
不用资源文件,又没有设置排序状态提示时的逻辑(已经注释掉了)#region 不用资源文件,又没有设置排序状态提示时的逻辑(已经注释掉了)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
/**///// 添加排序状态提示图标
InBlock.gif                    //Label lblSortDirection = new Label();
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    /**///// 被注释的代码使用的是webdings,但是FF不支持,所以放弃了
InBlock.gif                    
//// lblSortDirection.Font.Name = "webdings";
ExpandedSubBlockEnd.gif                    
//// lblSortDirection.EnableTheming = false;

InBlock.gif                    //if (sortDirection == SortDirection.Ascending)
InBlock.gif                    
//{
InBlock.gif                    
//    // lblSortDirection.Text = "5";
InBlock.gif                    
//    lblSortDirection.Text = "▲";
InBlock.gif                    
//}
InBlock.gif                    
//else
InBlock.gif                    
//{
InBlock.gif                    
//    // lblSortDirection.Text = "6";
InBlock.gif                    
//    lblSortDirection.Text = "▼";
InBlock.gif                    
//}
InBlock.gif                    
//tc.Controls.Add(lblSortDirection);
InBlock.gif
InBlock.gif                    
//if (this._sgv.SmartSorting.AllowMultiSorting)
InBlock.gif                    
//{
InBlock.gif                    
//    // 添加排序状态提示序号
InBlock.gif                    
//    tc.Controls.Add(new LiteralControl((sortOrderIndex + 1).ToString()));
InBlock.gif                    
//}
ExpandedSubBlockEnd.gif
                    #endregion

InBlock.gif
ExpandedSubBlockEnd.gif                }
 // if (sortOrderIndex != -1)
ExpandedSubBlockEnd.gif
            }
 // if (tc.Controls.Count > 0 && tc.Controls[0] is LinkButton)
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


/*正式版的实现 结束*/

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


控件开发
1、新建一个继承自GridView的类。

ExpandedBlockStart.gifContractedBlock.gif    /**/
/// <summary>
InBlock.gif    
/// 继承自GridView
ExpandedBlockEnd.gif    
/// </summary>

None.gif    [
None.gif    ToolboxData(
@"<{0}:SmartGridView runat=''server''></{0}:SmartGridView>"),
None.gif    ParseChildren(
true),
None.gif    PersistChildren(
false)
None.gif    ]
None.gif    
public class SmartGridView : GridView
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
ExpandedBlockEnd.gif    }

2、新建一个SortTip实体类,有4个属性,分别是降序提示图片、升序提示图片、降序提示文本和升序提示文本

None.gif
using System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
None.gif
using System.ComponentModel;
None.gif
None.gif
namespace YYControls.SmartGridView
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 排序提示类
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [TypeConverter(typeof(ExpandableObjectConverter))]
InBlock.gif    
public class SortTip
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string _sortDescImage;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 降序提示图片
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [
InBlock.gif        Description(
"降序提示图片"),
InBlock.gif        Category(
"扩展"),
InBlock.gif        Editor(
"System.Web.UI.Design.UrlEditor"typeof(System.Drawing.Design.UITypeEditor)),
InBlock.gif        DefaultValue(
""),
InBlock.gif        NotifyParentProperty(
true)
InBlock.gif        ]
InBlock.gif        
public string SortDescImage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _sortDescImage; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _sortDescImage = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string _sortAscImage;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 升序提示图片
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [
InBlock.gif        Description(
"升序提示图片"),
InBlock.gif        Category(
"扩展"),
InBlock.gif        Editor(
"System.Web.UI.Design.UrlEditor"typeof(System.Drawing.Design.UITypeEditor)),
InBlock.gif        DefaultValue(
""),
InBlock.gif        NotifyParentProperty(
true)
InBlock.gif        ]
InBlock.gif        
public string SortAscImage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _sortAscImage; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _sortAscImage = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string _sortDescText;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 降序提示文本
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [
InBlock.gif        Description(
"降序提示文本"),
InBlock.gif        Category(
"扩展"),
InBlock.gif        DefaultValue(
""),
InBlock.gif        NotifyParentProperty(
true)
InBlock.gif        ]
InBlock.gif        
public string SortDescText
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _sortDescText; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _sortDescText = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string _sortAscText;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 升序提示文本
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [
InBlock.gif        Description(
"升序提示文本"),
InBlock.gif        Category(
"扩展"),
InBlock.gif        DefaultValue(
""),
InBlock.gif        NotifyParentProperty(
true)
InBlock.gif        ]
InBlock.gif        
public string SortAscText
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _sortAscText; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _sortAscText = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// ToString()
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public override string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return "SortTip";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

3、在继承自GridView的那个类中加1个复杂对象属性,这个复杂对象就是第2步创建的那个SortTip

None.gif        
private SortTip _sortTip;
ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
InBlock.gif        
/// 排序提示信息
ExpandedBlockEnd.gif        
/// </summary>

None.gif        [
None.gif        Description(
"排序提示信息"),
None.gif        Category(
"扩展"),
None.gif        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
None.gif        PersistenceMode(PersistenceMode.InnerProperty)
None.gif        ]
None.gif        
public virtual SortTip SortTip
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_sortTip == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    _sortTip 
= new SortTip();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return _sortTip;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

4、重写OnRowDataBound实现给字段标题加上排序状态的功能。主要是根据GridView.SortExpression和GridView.SortDirection来实现这样的功能。

ExpandedBlockStart.gifContractedBlock.gif        /**/
/// <summary>
InBlock.gif        
/// OnRowDataBound
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <param name="e"></param>

None.gif        protected override void OnRowDataBound(GridViewRowEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
if (e.Row.RowType == DataControlRowType.Header)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// GridViewRow的每个TableCell
InBlock.gif
                for (int i = 0; i < e.Row.Cells.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// TableCell里有一个Control并且是LinkButton
InBlock.gif
                    if (e.Row.Cells[i].Controls.Count == 1 && e.Row.Cells[i].Controls[0is LinkButton)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
// LinkButton的命令参数等于排序字段
InBlock.gif
                        if (((LinkButton)e.Row.Cells[i].Controls[0]).CommandArgument == this.SortExpression)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            Image img 
= null;
InBlock.gif                            Label lbl 
= null;
InBlock.gif
InBlock.gif                            
// 升序
InBlock.gif
                            if (this.SortDirection == SortDirection.Ascending)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
// 升序图片
InBlock.gif
                                if (!String.IsNullOrEmpty(_sortTip.SortAscImage))
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    img 
= new Image();
InBlock.gif                                    img.ImageUrl 
= base.ResolveUrl(_sortTip.SortAscImage);
ExpandedSubBlockEnd.gif                                }

InBlock.gif                                
// 升序文字
InBlock.gif
                                if (!String.IsNullOrEmpty(_sortTip.SortAscText))
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    lbl 
= new Label();
InBlock.gif                                    lbl.Text 
= _sortTip.SortAscText;
ExpandedSubBlockEnd.gif                                }

ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
// 降序
InBlock.gif
                            else if (this.SortDirection == SortDirection.Descending)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
// 降序图片
InBlock.gif
                                if (!String.IsNullOrEmpty(_sortTip.SortDescImage))
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    img 
= new Image();
InBlock.gif                                    img.ImageUrl 
= base.ResolveUrl(_sortTip.SortDescImage);
ExpandedSubBlockEnd.gif                                }

InBlock.gif                                
// 降序文字
InBlock.gif
                                if (!String.IsNullOrEmpty(_sortTip.SortDescText))
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    lbl 
= new Label();
InBlock.gif                                    lbl.Text 
= _sortTip.SortDescText;
ExpandedSubBlockEnd.gif                                }

ExpandedSubBlockEnd.gif                            }

InBlock.gif
InBlock.gif                            
// TableCell里加上图片
InBlock.gif
                            if (img != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                e.Row.Cells[i].Controls.Add(img);
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
// TableCell里加上文字
InBlock.gif
                            if (lbl != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                e.Row.Cells[i].Controls.Add(lbl);
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
base.OnRowDataBound(e);
ExpandedBlockEnd.gif        }

None.gif


控件使用
添加这个控件到工具箱里,然后拖拽到webform上,设置其SortTip下的4个属性即可。SortAscImage是升序提示图片;SortAscText是升序提示文本;SortDescImage是降序提示图片;SortDescText是降序提示文本
ObjData.cs

None.gif
using System;
None.gif
using System.Data;
None.gif
using System.Configuration;
None.gif
using System.Web;
None.gif
using System.Web.Security;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.WebControls.WebParts;
None.gif
using System.Web.UI.HtmlControls;
None.gif
None.gif
using System.ComponentModel;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//// <summary>
InBlock.gif
/// OjbData 的摘要说明
ExpandedBlockEnd.gif
/// </summary>

None.gifpublic class OjbData
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public OjbData()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//
InBlock.gif        
// TODO: 在此处添加构造函数逻辑
InBlock.gif        
//
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    [DataObjectMethod(DataObjectMethodType.Select, 
true)]
InBlock.gif    
public DataTable Select()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        DataTable dt 
= new DataTable();
InBlock.gif        dt.Columns.Add(
"no"typeof(string));
InBlock.gif        dt.Columns.Add(
"name"typeof(string));
InBlock.gif
InBlock.gif        
for (int i = 0; i < 30; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataRow dr 
= dt.NewRow();
InBlock.gif            dr[
0= "no" + i.ToString().PadLeft(2''0'');
InBlock.gif            dr[
1= "name" + i.ToString().PadLeft(2''0'');
InBlock.gif
InBlock.gif            dt.Rows.Add(dr);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
return dt;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

Default.aspx

ExpandedBlockStart.gifContractedBlock.gif<%
dot.gif@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
None.gif
None.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
<html xmlns="http://www.w3.org/1999/xhtml">
None.gif
<head runat="server">
None.gif    
<title>无标题页</title>
None.gif
</head>
None.gif
<body>
None.gif    
<form id="form1" runat="server">
None.gif        
<div>
None.gif            
<yyc:SmartGridView ID="SmartGridView1" runat="server" AutoGenerateColumns="True"
None.gif                AllowSorting
="True" DataSourceID="ObjectDataSource1">
None.gif                
<Columns>
None.gif                    
<asp:ButtonField CommandName="ConfirmTest" Text="确认按钮测试" />
None.gif                
</Columns>
None.gif                
<SortTip SortAscImage="~/Images/asc.gif" SortDescImage="~/Images/desc.gif" />
None.gif            
</yyc:SmartGridView>
None.gif            
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select"
None.gif                TypeName
="OjbData"></asp:ObjectDataSource>
None.gif        
</div>
None.gif  
</form>
None.gif
</body>
None.gif
</html>


转载于:https://www.cnblogs.com/kwhei/archive/2008/10/01/1302948.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值