How To Make a Column in an ASPxGridView ReadOnly for Editing but Enabled when Inserting a New Record

The Situation:

You have a DevExpress ASPxGridView on your page with a column designated as “ReadOnly=true”. This works great when you are editing an existing record — it won’t allow you to change that field, but when you try to insert a NEW record, you can’t enter anything in the field.

Note: I don’t know if this happens under all circumstances, but for me, it happened for an ASPxGridView that is databound via code rather than from a datasource object on an aspx page.

A Solution:

The simple solution for this problem is to handle the “oncelleditorinitialize” event of the ASPxGridView.

Here is (simplified) ASPxGridView in my aspx page:

<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False"
	KeyFieldName="SettingName" onrowdeleting="ASPxGridView1_RowDeleting"
	onrowupdating="ASPxGridView1_RowUpdating" Theme="DevEx"
		<dx:GridViewCommandColumn VisibleIndex="0">
			<EditButton Visible="True" />
			<NewButton Visible="True" />
			<DeleteButton Visible="True" />
		<dx:GridViewDataTextColumn FieldName="SettingName" ReadOnly="true"
		<dx:GridViewDataTextColumn FieldName="SettingValue" VisibleIndex="2">

And here is the code called for the oncelleditorinitialize event:

protected void ASPxGridView1_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
	if (ASPxGridView1.IsNewRowEditing)
		if ((e.Column.FieldName) == "SettingName")
			e.Editor.ReadOnly = false;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s