This example handles the client-side FocusedCellChanging event to disable the cell edit action in batch edit mode based on a condition defined in code.
Limitation: This technique is not applicable if you set the EditMode property to Row. A user can focus and edit any cell in a row switched to edit mode except for cells that belong to read-only columns (the column's GridViewDataColumn.ReadOnly property is set to
true
).
Implementation Details
The following client-side FocusedCellChanging event handler sets the e.Cancel property to true
to cancel the focus action and subsequent edit operations for cells in specific columns and rows. The code uses the e.cellInfo event property to get information about the clicked cell.
JavaScriptfunction onFocusedCellChanging(s, e) {
if (e.cellInfo.column.name == "command") e.cancel = true;
else if (e.cellInfo.column.fieldName == "SupplierID") e.cancel = true;
else if (
e.cellInfo.column.fieldName == "UnitsInStock" &&
(e.cellInfo.rowVisibleIndex < 3 || e.cellInfo.rowVisibleIndex > 7)
)
e.cancel = true;
else if (
e.cellInfo.column.fieldName == "UnitPrice" &&
s.batchEditApi.GetCellValue(e.cellInfo.rowVisibleIndex, "UnitPrice") > 22
)
e.cancel = true;
}
Files to Look At
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)
Documentation
More Examples
- Grid View for ASP.NET Web Forms - A simple batch editing implementation
- Grid View for ASP.NET Web Forms - Editing an in-memory dataset
Example Code
ASPx<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.v22.1, Version=22.1.8.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web" TagPrefix="dx" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASPxGridView - How to use the client-side FocusedCellChanging</title>
<script type="text/javascript">
function onFocusedCellChanging(s, e) {
if (e.cellInfo.column.name == 'command')
e.cancel = true;
else if (e.cellInfo.column.fieldName == 'SupplierID')
e.cancel = true;
else if (e.cellInfo.column.fieldName == 'UnitsInStock' && (e.cellInfo.rowVisibleIndex < 3 || e.cellInfo.rowVisibleIndex > 7))
e.cancel = true;
else if (e.cellInfo.column.fieldName == 'UnitPrice' && s.batchEditApi.GetCellValue(e.cellInfo.rowVisibleIndex, 'UnitPrice') > 22)
e.cancel = true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="AccessDataSource1" KeyFieldName="ProductID"
OnBatchUpdate="ASPxGridView1_BatchUpdate">
<Columns>
<dx:GridViewCommandColumn ShowEditButton="true" VisibleIndex="0" Name="command">
</dx:GridViewCommandColumn>
<dx:GridViewDataTextColumn FieldName="ProductID" ReadOnly="True" VisibleIndex="1">
<EditFormSettings Visible="False"></EditFormSettings>
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="2">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="SupplierID" VisibleIndex="3">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="CategoryID" VisibleIndex="4">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="UnitPrice" VisibleIndex="5">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="UnitsInStock" VisibleIndex="6">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="UnitsOnOrder" VisibleIndex="6">
</dx:GridViewDataTextColumn>
</Columns>
<SettingsEditing Mode="Batch"></SettingsEditing>
<ClientSideEvents FocusedCellChanging="onFocusedCellChanging" />
</dx:ASPxGridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/nwind.mdb"
SelectCommand="SELECT [ProductID], [ProductName], [SupplierID], [CategoryID], [UnitPrice], [UnitsInStock], [UnitsOnOrder] FROM [Products]">
</asp:AccessDataSource>
</div>
</form>
</body>
</html>
C#using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Solution {
public partial class Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
protected void ASPxGridView1_BatchUpdate(object sender, DevExpress.Web.Data.ASPxDataBatchUpdateEventArgs e) {
//Data editing is not allowed in the example
e.Handled = true;
}
}
}