Example E5116
Visible to All Users

Grid View for ASP.NET WebForms - How to conditionally make cell editors read-only

Implementation Details

This example demonstrates how to set a grid cell editor's ReadOnly property based on the grid's state. The example application allows you to edit a field value in a new row, but the editor becomes read-only when you try to edit an existing row.

The ReadOnly property is set in the ASPxGridView.CellEditorInitialize event handler based on the value of the IsNewRowEditing property.

C#
protected void gridView_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e) { ASPxGridView grid = sender as ASPxGridView; if (e.Column.FieldName == "CategoryID") e.Editor.ReadOnly = !grid.IsNewRowEditing; }

Files to Look At

Documentation

More Examples

Example Code

Solution/Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Solution.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>How to conditionally make cell editors read-only</title> </head> <body> <form id="form1" runat="server"> <dx:ASPxGridView ID="gridView" runat="server" AutoGenerateColumns="False" DataSourceID="ads" KeyFieldName="CategoryID" ClientInstanceName="gridView" OnCellEditorInitialize="gridView_CellEditorInitialize"> <Columns> <dx:GridViewCommandColumn ShowEditButton="True" ShowNewButtonInHeader="True" VisibleIndex="0"> </dx:GridViewCommandColumn> <dx:GridViewDataTextColumn FieldName="CategoryID" VisibleIndex="1"> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn FieldName="CategoryName" VisibleIndex="2"> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn FieldName="Description" VisibleIndex="3"> </dx:GridViewDataTextColumn> </Columns> </dx:ASPxGridView> <asp:AccessDataSource ID="ads" runat="server" DataFile="~/App_Data/nwind.mdb" SelectCommand="SELECT * FROM [Categories]"> </asp:AccessDataSource> </form> </body> </html>
Solution/Default.aspx.cs(vb)
C#
using DevExpress.Web; using System; namespace Solution { public partial class Default : System.Web.UI.Page { protected void gridView_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e) { ASPxGridView grid = sender as ASPxGridView; if (e.Column.FieldName == "CategoryID") e.Editor.ReadOnly = !grid.IsNewRowEditing; } } }

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.