DevExpress ASPxGridView 使用文档五:事件
转载请注明出处:http://surfsky.cnblogs.com/
---------------------------------------------------------
-- ASPxGridView 事件
---------------------------------------------------------
ASPxGridView
默认是以callback方式(ajax方式)传递数据给服务器来实现局部刷新功能的
若要改为postback方式,可设置 EnableCallBacks = "false"
---------------------------------------------------------
展示视图
---------------------------------------------------------
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, "changePercent") as Label;
decimal change = (decimal)grid.GetRowValues(e.VisibleIndex, "Change");
label.Text = string.Format("{0:p}", change);
// 设置模板列image控件的图像
System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)grid.FindRowCellTemplateControl(e.VisibleIndex, null, "changeImage");
img.Visible = false;
if(change != 0) {
img.Visible = true;
img.ImageUrl = change < 0 ? "~/Images/arRed.gif" : "~/Images/arGreen.gif";
label.ForeColor = change < 0 ? Color.Red : Color.Green;
}
}
HtmlRowPrepared(行准备?可在此设置行的展示效果,如背景)
protected void grid_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e)
{
bool hasError = e.GetValue("FirstName").ToString().Length <= 1;
hasError = hasError || e.GetValue("LastName").ToString().Length <= 1;
hasError = hasError || !e.GetValue("Email").ToString().Contains("@");
hasError = hasError || (int)e.GetValue("Age") < 18;
DateTime arrival = (DateTime)e.GetValue("ArrivalDate");
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 == "Total")
{
decimal price = (decimal)e.GetListSourceFieldValue("UnitPrice");
int quantity = Convert.ToInt32(e.GetListSourceFieldValue("Quantity"));
e.Value = price * quantity;
}
}
CustomColumnDisplayText(定制列文本展示)
protected void grid_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs e)
{
if(object.Equals(e.Column, grid.Columns["Size"]))
e.DisplayText = GetSizeDisplayText(e.Value);
}
SummaryDisplayText(合计行文本展示)
protected void grid_SummaryDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewSummaryDisplayTextEventArgs e) {
if(e.Item.FieldName == "Size") {
e.Text = GetSizeDisplayText(e.Value);
}
}
HeaderFilterFillItems(自定义过滤器处理逻辑)
protected void grid_HeaderFilterFillItems(object sender, ASPxGridViewHeaderFilterEventArgs e)
{
if(object.Equals(e.Column, grid.Columns["Total"])) {
PrepareTotalFilterItems(e);
return;
}
if(object.Equals(e.Column, grid.Columns["Quantity"])) {
PrepareQuantityFilterItems(e);
return;
}
}
---------------------------------------------------------
回调处理
---------------------------------------------------------
CustomCallback(Ajax 回调处理)
<select id="selGridLayout" onchange="grid.PerformCallback(this.value);" >
<option selected="selected" value="0">Country</option>
<option value="1">Country, City</option>
<option value="2">Company Name</option>
</select>
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 != "Copy") 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 == "City")
{
string country = (string)grid.GetRowValuesByKeyValue(e.KeyValue, "Country");
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.ASPxStartRowEditingEventArgs e)
{
if(!grid.IsNewRowEditing) {
grid.DoRowValidation();
}
}
RowValidating (行数据验证)
protected void grid_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
{
foreach(GridViewColumn column in grid.Columns) {
GridViewDataColumn dataColumn = column as GridViewDataColumn;
if(dataColumn == null) continue;
if(e.NewValues[dataColumn.FieldName] == null) {
e.Errors[dataColumn] = "Value can't be null.";
}
}
if(e.Errors.Count > 0) e.RowError = "Please, fill all fields.";
if(e.NewValues["FirstName"] != null && e.NewValues["FirstName"].ToString().Length < 2) {
AddError(e.Errors, grid.Columns["FirstName"], "First Name must be at least two characters long.");
}
if(e.NewValues["LastName"] != null && e.NewValues["LastName"].ToString().Length < 2) {
AddError(e.Errors, grid.Columns["LastName"], "Last Name must be at least two characters long.");
}
if(e.NewValues["Email"] != null && !e.NewValues["Email"].ToString().Contains("@")) {
AddError(e.Errors, grid.Columns["Email"], "Invalid e-mail.");
}
int age = 0;
int.TryParse(e.NewValues["Age"] == null ? string.Empty : e.NewValues["Age"].ToString(), out age);
if(age < 18) {
AddError(e.Errors, grid.Columns["Age"], "Age must be greater than or equal 18.");
}
DateTime arrival = DateTime.MinValue;
DateTime.TryParse(e.NewValues["ArrivalDate"] == null ? string.Empty : e.NewValues["ArrivalDate"].ToString(), out arrival);
if(DateTime.Today.Year != arrival.Year || DateTime.Today.Month != arrival.Month) {
AddError(e.Errors, grid.Columns["ArrivalDate"], "Arrival date is required and must belong to the current month.");
}
if(string.IsNullOrEmpty(e.RowError) && e.Errors.Count > 0) e.RowError = "Please, correct all errors.";
}
转载请注明出处:http://surfsky.cnblogs.com