Example T496531
Visible to All Users

Grid View for ASP.NET Web Forms - Prevent the cell edit action on the client in batch edit mode

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.

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; }

Files to Look At

Documentation

More Examples

Example Code

Solution/Default.aspx
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>
Solution/Default.aspx.cs(vb)
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; } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.