说实话,对于这种控件类的使用和学习最快的入门 方式就是边看官方示例,边整理编程文档,此后基本上就可以脱离示例,看文档就可以编程了。此系列文档是ASPxGridView的编程有效参考,前前后后 整理了很多回了,给有需要的人使用。
功能概述
简单示例
(注意:FieldName 是区分大小写的)
grid.DataSource = dt;
grid.DataBind();
小贴士
(1)在web.config里面做配置
...
DevExpress 的web控件都处于不同的命名空间,使用起来很不方便。
经过这样处理后,统一了DevExpress web 控件的标签前缀,方便多了,如:
(2)在CS 文件加上这几个using,有效减少页面代码
using DevExpress.Web.Data;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
(3)常用的主题设置
PopupEditFormModal= "true"
PopupEditFormHorizontalAlign= "WindowCenter"
PopupEditFormVerticalAlign= "WindowCenter"
PopupEditFormAllowResize= "true"
/ >
PopupEditFormCaption= "编辑"
ConfirmDelete= "确定删除?"
/ >
(4)ASPxGridView 的属性设置方式比较独特
既可以传统的层层嵌套,如:
也可以简化为“组合属性名”(姑且这样称呼吧)的方式:
好处是可以一行摆平,坏处是这些组合属性名的名称很长很长很长...
说实话,个人认为
ASPxGridView 属性设计得还是蛮严谨的,其属性层层嵌套,含义明确。
(而另外一个类似的产品,Infragistic公司的UltraGrid 属性设计则是完全失控了)
如果是winform倒无所谓,全部在cs代码中设置了,但作为aspnet控件的话写出来的层次就会很冗长
故我考虑这是devexpress公司为aspnet控件设计出来的一种折衷方案,允许以组合属性的方式来设置。
实际使用情况呢,有时候我觉得很方便,有时候觉得还是很冗长,看情况用吧。
几个常用属性
IsEditing : 是否处于编辑状态
IsNewRowEditing : 是否是新建行的编辑状态
几个常用方法
获取单元格的值
decimal change = (decimal)grid.GetRowValues(e.VisibleIndex, "Change");
获取模板中的控件
Label label = grid.FindRowCellTemplateControl(e.VisibleIndex, null, "changePercent") as Label;
概述设置(Settings)
ShowGroupPanel= "True" : 分组面板
ShowFooter= "True" : 脚注面板
ShowFilterRow= "True" : 过滤器行
ShowHeaderFilterButton= "true" : 表头过滤按钮
ShowGroupFooter= "VisibleAlways" : 分组脚注面板 Hidden | VisibleIfExpand | VisibleAlways
ShowPreview= "true" : 预览面板
ShowVerticalScrollBar= "True" : 垂直滚动条
VerticalScrollableHeight= "250" : 垂直滚动条
/ >
行为设置(SettingsBehavior)
ColumnResizeMode= "Control" : 列宽度调整模式
AllowFocusedRow= "True" : 鼠标点击选择行
/ >
分页(SettingsPager)
Mode= "ShowAllRecords" : 展示模式
SEOFriendly= "Enabled" : Search engine friendly
Position= "TopAndBottom" : 分页控件位置
>
文本设置(SettingsText)
EmptyDataRow= "无数据"
PopupEditFormCaption= "编辑"
ConfirmDelete= "确定删除?"
/ >
Loading 面板设置(SettingsLoadingPanel)
编辑视图设置(SettingsEditing)
NewItemRowPosition = "Bottom"
Mode = "PopupEditForm"
/ >
编辑模式 SettingsEditing.Mode
EditForm : 当前行转化为表单,嵌入在行中
EditFormAndDisplayRow : 同EditForm,但保留当前行
Inline : 在当前行现场编辑
PopupEditForm : 弹出窗口编辑
集中式样式
列样式
数字日期格式
金额:
时间 :
图像
间隔分组:将时间日期字段按个性分组,如年、月、日、周、季度、上周、下周.....
汇总
分组汇总
排序
默认就是支持点击标题排序的,不过注意Page_Load中不能用 IsPostBack,如:
protected void Page_Load(object sender, EventArgs e)
{
grid.DataSource = ....;
grid.DataBind();
}
ASPxGridView 在每次请求来的时候先绑定,然后再根据排序或分页请求,过滤数据后展示给用户
当然,你也可以像 GridView 那样在服务器端自己写排序或者分页代码.
指定列的排序顺序
用代码指定排序列集合
grid.GroupSummarySortInfo.Clear();
DevExpress.Data.ColumnSortOrder sortOrder = DevExpress.Data.ColumnSortOrder.Ascending; //Descending
grid.GroupSummarySortInfo.AddRange(new ASPxGroupSummarySortInfo( "Country", grid.GroupSummary["Total"], sortOrder));
导出文件
this.gridExporter.WritePdfToResponse();
this.gridExporter.WriteXlsToResponse();
this.gridExporter.WriteRtfToResponse();
this.gridExporter.WriteCsvToResponse();
服务器端杂代码
grid.BeginUpdate();
grid.ClearSort();
grid.GroupBy((GridViewDataColumn)grid.Columns[ "Country"]);
grid.EndUpdate();
grid.ExpandAll();
grid.FindRowCellTemplateControl(e.VisibleIndex, null, "changePercent") as Label;