This example demonstrates how to populate a GridViewDataComboBoxColumn's cell editors at runtime in batch edit mode.
Implementation Details
In batch edit mode, the Grid View does not send requests to the server when a cell editor is activated. Therefore, it is not possible to use the CellEditorInitialize event handler on the server to populate a combo box in each row.
To overcome this limitation, use the combo box editor's callback to populate the editor with items.
- Handle the grid's CellEditorInitialize event. In the event handler, assign a new handler to the combo box cell editor's Callback event:C#
protected void Grid_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e) { if (e.Column.FieldName == "C3") { ASPxComboBox combo = e.Editor as ASPxComboBox; combo.Callback += combo_Callback; } }
- Call the combo box editor's PerformCallback method from the client ASPxClientGridView.BatchEditStartEditing event handler. Pass the current row's visible index as the callback parameter:ASPx
<dx:ASPxGridView ID="Grid" runat="server" KeyFieldName="ID" ... > ... <ClientSideEvents BatchEditStartEditing="OnBatchEditStartEditing" /> </dx:ASPxGridView>
Codefunction OnBatchEditStartEditing(s, e) { cmb.PerformCallback(); }
- Add items to the combo box in the Callback event handler. You can get the current row index from
e.Parameter
.C#void combo_Callback(object sender, DevExpress.Web.CallbackEventArgsBase e) { ASPxComboBox combo = sender as ASPxComboBox; for (int i = 0; i < 10; i++) { combo.Items.Add(string.Format("Row_{0} Item_{1}", e.Parameter, i), i); } }
Files to Review
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)
Documentation
More Examples
- Grid View for ASP.NET Web Forms - How to bind the GridViewDataComboBoxColumn edit form editor at runtime
- Grid View for ASP.NET Web Forms - How to cache data on the client
Example Code
ASPx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.v23.1, Version=23.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web" TagPrefix="dx" %>
<!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>
<script type="text/javascript">
function OnBatchEditStartEditing(s, e) {
cmb.PerformCallback();
}
function OnEndCallback(s, e) {
s.SetValue(s.GetValue());
}
</script>
</head>
<body>
<form id="frmMain" runat="server">
<dx:ASPxCheckBox ID="BatchUpdateCheckBox" runat="server" Text="Handle BatchUpdate event"
AutoPostBack="true" />
<dx:ASPxGridView ID="Grid" runat="server" KeyFieldName="ID" OnBatchUpdate="Grid_BatchUpdate"
OnRowInserting="Grid_RowInserting" OnRowUpdating="Grid_RowUpdating" OnRowDeleting="Grid_RowDeleting" OnCellEditorInitialize="Grid_CellEditorInitialize">
<Columns>
<dx:GridViewCommandColumn ShowNewButtonInHeader="true" ShowDeleteButton="true" />
<dx:GridViewDataColumn FieldName="C1" />
<dx:GridViewDataSpinEditColumn FieldName="C2" />
<dx:GridViewDataComboBoxColumn FieldName="C3" >
<PropertiesComboBox ClientInstanceName="cmb">
<ClientSideEvents EndCallback="OnEndCallback" />
</PropertiesComboBox>
</dx:GridViewDataComboBoxColumn>
<dx:GridViewDataCheckColumn FieldName="C4" />
</Columns>
<ClientSideEvents BatchEditStartEditing="OnBatchEditStartEditing" />
<SettingsEditing Mode="Batch" />
</dx:ASPxGridView>
</form>
</body>
</html>
C#using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using DevExpress.Web.Data;
using DevExpress.Web;
public partial class _Default : System.Web.UI.Page {
protected List<GridDataItem> GridData {
get {
var key = "34FAA431-CF79-4869-9488-93F6AAE81263";
if(!IsPostBack || Session[key] == null)
Session[key] = Enumerable.Range(0, 100).Select(i => new GridDataItem {
ID = i,
C1 = i % 2,
C2 = i * 0.5 % 3,
C3 = "C3 " + i,
C4 = i % 2 == 0
}).ToList();
return (List<GridDataItem>)Session[key];
}
}
protected void Page_Load(object sender, EventArgs e) {
Grid.DataSource = GridData;
Grid.DataBind();
}
protected void Grid_RowInserting(object sender, ASPxDataInsertingEventArgs e) {
InsertNewItem(e.NewValues);
CancelEditing(e);
}
protected void Grid_RowUpdating(object sender, ASPxDataUpdatingEventArgs e) {
UpdateItem(e.Keys, e.NewValues);
CancelEditing(e);
}
protected void Grid_RowDeleting(object sender, ASPxDataDeletingEventArgs e) {
DeleteItem(e.Keys, e.Values);
CancelEditing(e);
}
protected void Grid_BatchUpdate(object sender, ASPxDataBatchUpdateEventArgs e) {
if(!BatchUpdateCheckBox.Checked)
return;
foreach(var args in e.InsertValues)
InsertNewItem(args.NewValues);
foreach(var args in e.UpdateValues)
UpdateItem(args.Keys, args.NewValues);
foreach(var args in e.DeleteValues)
DeleteItem(args.Keys, args.Values);
e.Handled = true;
}
protected GridDataItem InsertNewItem(OrderedDictionary newValues) {
var item = new GridDataItem() { ID = GridData.Count };
LoadNewValues(item, newValues);
GridData.Add(item);
return item;
}
protected GridDataItem UpdateItem(OrderedDictionary keys, OrderedDictionary newValues) {
var id = Convert.ToInt32(keys["ID"]);
var item = GridData.First(i => i.ID == id);
LoadNewValues(item, newValues);
return item;
}
protected GridDataItem DeleteItem(OrderedDictionary keys, OrderedDictionary values) {
var id = Convert.ToInt32(keys["ID"]);
var item = GridData.First(i => i.ID == id);
GridData.Remove(item);
return item;
}
protected void LoadNewValues(GridDataItem item, OrderedDictionary values) {
item.C1 = Convert.ToInt32(values["C1"]);
item.C2 = Convert.ToDouble(values["C2"]);
item.C3 = Convert.ToString(values["C3"]);
item.C4 = Convert.ToBoolean(values["C4"]);
}
protected void CancelEditing(CancelEventArgs e) {
e.Cancel = true;
Grid.CancelEdit();
}
public class GridDataItem {
public int ID { get; set; }
public int C1 { get; set; }
public double C2 { get; set; }
public string C3 { get; set; }
public bool C4 { get; set; }
}
protected void Grid_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e) {
if (e.Column.FieldName == "C3") {
ASPxComboBox combo = e.Editor as ASPxComboBox;
combo.Callback += combo_Callback;
}
}
void combo_Callback(object sender, DevExpress.Web.CallbackEventArgsBase e) {
ASPxComboBox combo = sender as ASPxComboBox;
for (int i = 0; i < 10; i++)
{
combo.Items.Add(string.Format("Row_{0} Item_{1}", e.Parameter, i), i);
}
}
}