if (!grid.IsEditing && e.RowType == DevExpress.Web.ASPxGridView.GridViewRowType.Data) {
// 操作 Label 控件
Label label = grid.FindRowCellTemplateControl(e.VisibleIndex, null, \ent\as Label;
decimal change = (decimal)grid.GetRowValues(e.VisibleIndex, \ label.Text = string.Format(\change); // 操作 Image 控件
System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)grid.FindRowCellTemplateControl(e.VisibleIndex, null, \ img.Visible = false; if(change != 0) {
img.Visible = true;
img.ImageUrl = change < 0 ? \: \\
label.ForeColor = change < 0 ? Color.Red : Color.Green; } } }
注:模板列中的事件如何写?手工写事件,如btn.OnClick += ...; 注:视图模板请参考文档《ASPxGridView.Templates》
ASPxGridView 事件
RowCreated(创建行数据时触发,类似 GridView 的 DataItemCreate 事件)
protected void grid_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e) {
if(e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.Data) return; // 设置模板列lable控件值
Label label = grid.FindRowCellTemplateControl(e.VisibleIndex, null, \\as Label;
decimal change = (decimal)grid.GetRowValues(e.VisibleIndex, \ label.Text = string.Format(\change); // 设置模板列image控件的图像
System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)grid.FindRowCellTemplateControl(e.VisibleIndex, null, \
img.Visible = false; if(change != 0) { img.Visible = true;
img.ImageUrl = change < 0 ? \: \ label.ForeColor = change < 0 ? Color.Red : Color.Green; } }
HtmlRowPrepared(行准备?可在此设置行的展示效果,如背景)
protected void grid_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e) {
bool hasError = e.GetValue(\<= 1; hasError = hasError || e.GetValue(\<= 1; hasError = hasError || !e.GetValue(\ hasError = hasError || (int)e.GetValue(\< 18; DateTime arrival = (DateTime)e.GetValue(\
hasError = hasError || DateTime.Today.Year != arrival.Year || DateTime.Today.Month != arrival.Month; if(hasError) {
e.Row.ForeColor = System.Drawing.Color.Red; } }
UnboundColumnData (非绑定列数据填充)
protected void grid_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e) {
if(e.Column.FieldName == \ {
decimal price = (decimal)e.GetListSourceFieldValue(\ int quantity = Convert.ToInt32(e.GetListSourceFieldValue(\ e.Value = price * quantity; } }
CustomColumnDisplayText(定制列文本展示)
protected void grid_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs e) {
if(object.Equals(e.Column, grid.Columns[\
e.DisplayText = GetSizeDisplayText(e.Value); }
SummaryDisplayText(合计行文本展示)
protected void grid_SummaryDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewSummaryDisplayTextEventArgs e) { if(e.Item.FieldName == \{ e.Text = GetSizeDisplayText(e.Value); } }
HeaderFilterFillItems(自定义过滤器处理逻辑)
protected void grid_HeaderFilterFillItems(object sender, ASPxGridViewHeaderFilterEventArgs e) {
if(object.Equals(e.Column, grid.Columns[\{ PrepareTotalFilterItems(e); return; }
if(object.Equals(e.Column, grid.Columns[\{ PrepareQuantityFilterItems(e); return; } }
--------------------------------------------------------- 回调处理
--------------------------------------------------------- CustomCallback(Ajax 回调处理)
protected void grid_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e) {
int layoutIndex = -1;
if(int.TryParse(e.Parameters, out layoutIndex)) ApplyLayout(layoutIndex); // 更换布局 }
CustomButtonCallback(定制按钮的ajax回调处理)
protected void grid_CustomButtonCallback(object sender, ASPxGridViewCustomButtonCallbackEventArgs e) {
if(e.ButtonID != \return; copiedValues = new Hashtable();
foreach(string fieldName in copiedFields)
copiedValues[fieldName] = grid.GetRowValues(e.VisibleIndex, fieldName);
grid.AddNewRow(); }
--------------------------------------------------------- 编辑视图
--------------------------------------------------------- InitNewRow(新建行的数据初始化处理)
protected void grid_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e) {
if(copiedValues == null) return;
foreach(string fieldName in copiedFields) {
e.NewValues[fieldName] = copiedValues[fieldName]; } }
CellEditorInitialize(编辑器初始化)
protected void grid_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e) {
if(grid.IsEditing && !grid.IsNewRowEditing && e.Column.FieldName == \ {
string country = (string)grid.GetRowValuesByKeyValue(e.KeyValue, \ ASPxComboBox combo = e.Editor as ASPxComboBox; FillCityCombo(combo, country);
combo.Callback += new CallbackEventHandlerBase(cmbCity_OnCallback); } }
StartRowEditing(开始编辑)
protected void grid_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartR