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
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)
Documentation
More Examples
Example Code
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>
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;
}
}
}