This example demonstrates how to configure the grid's cell edit functionality to allow a user to edit multiple selected rows in a single edit form.
Overview
Create the Grid View control and add a button to the page. Handle the button's client-side Click
event and do the following in the handler:
- Call the grid's GetSelectedRowCount method to determine whether any grid rows are selected.
- Call the grid's
PerformCallback
method to send a callback to the server with a custom parameter (StartEditing).
ASPx<dx:ASPxGridView id="ASPxGridView1" runat="server" AutoGenerateColumns="False" ClientInstanceName="grid" KeyFieldName="ID" OnCellEditorInitialize="ASPxGridView1_CellEditorInitialize" OnCustomCallback="ASPxGridView1_CustomCallback" OnRowUpdating="ASPxGridView1_RowUpdating" OnStartRowEditing="ASPxGridView1_StartRowEditing">
<Columns>
<dx:GridViewCommandColumn ShowSelectCheckbox="True" VisibleIndex="0" />
<!-- ... -->
</Columns>
<SettingsEditing Mode="PopupEditForm" EditFormColumnCount="1" PopupEditFormModal="True" ... />
</dx:ASPxGridView>
<dx:ASPxButton ID="ASPxButton1" runat="server" AutoPostBack="False" Text="Edit">
<ClientSideEvents Click="function(s, e) {
if(grid.GetSelectedRowCount() == 0) alert('No grid row is selected.');
else grid.PerformCallback('StartEditing');
}" />
</dx:ASPxButton>
Handle the grid's server-side CustomCallback event. In the handler, define the last selected row and use the row's visible index to start editing.
C#protected void ASPxGridView1_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e){
ASPxGridView grid = sender as ASPxGridView;
if (e.Parameters == "StartEditing"){
object lastSelectedRowKeyValue = grid.GetSelectedFieldValues("ID")[grid.Selection.Count - 1];
int lastSelectedRowVisibleIndex = grid.FindVisibleIndexByKeyValue(lastSelectedRowKeyValue);
grid.StartEdit(lastSelectedRowVisibleIndex);
}
}
Handle the grid's server-side CellEditorInitialize event. In the handler, determine whether the edit value is common for all rows within the specified column. Based on a result, assign a new value to the editor.
C#protected void ASPxGridView1_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridViewEditorEventArgs e){
ASPxGridView grid = sender as ASPxGridView;
if (!(grid.IsEditing && isEditing)) return;
if (grid.Selection.Count == 1 || e.Column.ReadOnly) return;
e.Editor.Value = IsCommonValueForAllSelectedRows(e.Column, e.Value) ? e.Value : null;
}
private bool IsCommonValueForAllSelectedRows(DevExpress.Web.GridViewDataColumn column, object value){
bool res = true;
List<object> selectedRowValues = ASPxGridView1.GetSelectedFieldValues(column.FieldName);
for (int i = 0; i < selectedRowValues.Count; i++){
if (selectedRowValues[i].ToString() != value.ToString()){
res = false;
break;
}
}
return res;
}
To update grid data based on new edit values, handle the grid's server-side RowUpdating event. In the handler, use the newValues argument property to assign new values to modified cells or restore initial values.
C#protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e){
DataTable dt = Session["GridDataSource"] as DataTable;
if (ASPxGridView1.Selection.Count == 1){
DataRow row = dt.Rows.Find(e.Keys["ID"]) as DataRow;
row["Song"] = e.NewValues["Song"];
row["Genre"] = e.NewValues["Genre"];
}
if (ASPxGridView1.Selection.Count > 1){
List<object> selectedRowKeys = ASPxGridView1.GetSelectedFieldValues("ID");
for (int i = 0; i < selectedRowKeys.Count; i++){
DataRow row = dt.Rows.Find(selectedRowKeys[i]) as DataRow;
row["Song"] = (e.NewValues["Song"] == null) ? row["Song"] : e.NewValues["Song"];
row["Genre"] = (e.NewValues["Genre"] == null) ? row["Genre"] : e.NewValues["Genre"];
continue;
}
}
dt.AcceptChanges();
Session["GridDataSource"] = dt;
// ...
}
Files to Review
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)
Example Code
ASPx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.v13.1, Version=13.1.14.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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
Select one or several grid rows and click the Edit button to modify row values.<br /><br />
<dx:aspxgridview id="ASPxGridView1" runat="server" AutoGenerateColumns="False" ClientInstanceName="grid" KeyFieldName="ID" OnCellEditorInitialize="ASPxGridView1_CellEditorInitialize" OnCustomCallback="ASPxGridView1_CustomCallback" OnRowUpdating="ASPxGridView1_RowUpdating" OnStartRowEditing="ASPxGridView1_StartRowEditing">
<Columns>
<dx:GridViewCommandColumn ShowSelectCheckbox="True" VisibleIndex="0">
</dx:GridViewCommandColumn>
<dx:GridViewDataTextColumn FieldName="ID" VisibleIndex="1" ReadOnly="True">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="Song" VisibleIndex="2">
<PropertiesTextEdit NullText="Type new value here">
</PropertiesTextEdit>
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="Genre" VisibleIndex="3">
<PropertiesTextEdit NullText="Type new value here">
</PropertiesTextEdit>
</dx:GridViewDataTextColumn>
</Columns>
<SettingsEditing Mode="PopupEditForm" EditFormColumnCount="1" PopupEditFormModal="True" PopupEditFormHorizontalAlign="WindowCenter" PopupEditFormVerticalAlign="WindowCenter" />
</dx:aspxgridview>
<br />
<dx:ASPxButton ID="ASPxButton1" runat="server" AutoPostBack="False" Text="Edit">
<ClientSideEvents Click="function(s, e) {
if(grid.GetSelectedRowCount() == 0) alert('No grid row is selected.');
else grid.PerformCallback('StartEditing');
}" />
</dx:ASPxButton>
</form>
</body>
</html>
C#using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.Web;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
bool isEditing = false;
protected void Page_Load(object sender, EventArgs e){
//Create the grid's data source on the first load
if (!IsPostBack && !IsCallback) Session["GridDataSource"] = CreateDataSource();
//Populate the grid with data on each page load
if (Session["GridDataSource"] != null){
ASPxGridView1.DataSource = Session["GridDataSource"] as DataTable;
ASPxGridView1.DataBind();
}
}
private DataTable CreateDataSource(){
DataTable dataTable = new DataTable("MyTable");
dataTable.Columns.Add("ID", typeof(int));
dataTable.Columns.Add("Song", typeof(string));
dataTable.Columns.Add("Genre", typeof(string));
dataTable.PrimaryKey = new DataColumn[] { dataTable.Columns["ID"] };
for (int i = 0; i < 15; i++)
{
dataTable.Rows.Add(new object[] { i, "Song" + i.ToString(), "Rock" });
}
return dataTable;
}
protected void ASPxGridView1_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e){
ASPxGridView grid = sender as ASPxGridView;
//Switch the grid to edit mode for the last selected row
if (e.Parameters == "StartEditing"){
object lastSelectedRowKeyValue = grid.GetSelectedFieldValues("ID")[grid.Selection.Count - 1];
int lastSelectedRowVisibleIndex = grid.FindVisibleIndexByKeyValue(lastSelectedRowKeyValue);
grid.StartEdit(lastSelectedRowVisibleIndex);
}
}
protected void ASPxGridView1_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e){
isEditing = true;
}
protected void ASPxGridView1_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridViewEditorEventArgs e){
ASPxGridView grid = sender as ASPxGridView;
//Make sure that the code initializing editor values in EditForm is executed only once - when switching the grid to edit mode
if (!(grid.IsEditing && isEditing)) return;
if (grid.Selection.Count == 1 || e.Column.ReadOnly) return;
//Initialize an editor's value depending upon the value's uniqueness within a column
e.Editor.Value = IsCommonValueForAllSelectedRows(e.Column, e.Value) ? e.Value : null;
}
private bool IsCommonValueForAllSelectedRows(DevExpress.Web.GridViewDataColumn column, object value){
//Determine whether the passed value is common for all rows within the specified column
bool res = true;
List<object> selectedRowValues = ASPxGridView1.GetSelectedFieldValues(column.FieldName);
for (int i = 0; i < selectedRowValues.Count; i++){
if (selectedRowValues[i].ToString() != value.ToString()){
res = false;
break;
}
}
return res;
}
protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e){
DataTable dt = Session["GridDataSource"] as DataTable;
//Modify row values after a single grid row has been edited
if (ASPxGridView1.Selection.Count == 1){
DataRow row = dt.Rows.Find(e.Keys["ID"]) as DataRow;
row["Song"] = e.NewValues["Song"];
row["Genre"] = e.NewValues["Genre"];
}
//Modify row values after multiple grid rows have been edited
if (ASPxGridView1.Selection.Count > 1){
//Obtain key field values of the selected rows
List<object> selectedRowKeys = ASPxGridView1.GetSelectedFieldValues("ID");
for (int i = 0; i < selectedRowKeys.Count; i++){
//Find a row in the data table by the row's key field value
DataRow row = dt.Rows.Find(selectedRowKeys[i]) as DataRow;
//Modify data rows by leaving old values (if the EditForm's editors are left blank) or saving new values entered into editors
row["Song"] = (e.NewValues["Song"] == null) ? row["Song"] : e.NewValues["Song"];
row["Genre"] = (e.NewValues["Genre"] == null) ? row["Genre"] : e.NewValues["Genre"];
continue;
}
}
dt.AcceptChanges();
Session["GridDataSource"] = dt;
//Close the grid's EditForm and avoid default update processing from being executed
e.Cancel = true;
ASPxGridView1.CancelEdit();
}
}