Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

ASPxGridView - How to make some field editors read-only when editing a row and allow their editing when adding a new row

$
0
0

This example demonstrates how to set the editor's ReadOnly property based on the grid's state. I.e. it is possible can edit field value while adding a new row, but this editor becomes ReadOnly if we trying to edit existing row.


It is used the ASPxGridView.CellEditorInitialize event to implement this scenario:

[C#]
protectedvoidgridView_CellEditorInitialize(objectsender,DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgse){ASPxGridViewgrid=senderasASPxGridView;if(e.Column.FieldName=="CategoryID")e.Editor.ReadOnly=!grid.IsNewRowEditing;}

Question Comments

Added By: Ju Yeong Jeong at: 1/15/2015 1:00:32 AM    

It's so good example.

Thank you so much !!!

[ASPX]

<dx:ASPxGridView ID="grid" ClientInstanceName="grid" ....
KeyFieldName="MST_CD" OnCellEditorInitialize="grid_CellEditorInitialize">

<dx:ASPxGridView ID="gridDetail" ClientInstanceName="gridDetail" ....
KeyFieldName="MST_CD;CD" OnCellEditorInitialize="grid_CellEditorInitialize">

[C#]
   protected DataTable dtGrid = null;
   protected DataTable dtGridDetail = null;

   protected void Page_Load(object sender, EventArgs e)
   {
       if (! Page.IsPostBack)
       {
               //GET DATATABLE
               dtGrid = dataset.Tables[0];
               //SET PRIMARY KEY(S)
               dtGrid.PrimaryKey = new DataColumn[] { dtGrid.Columns["MST_CD"] };
               //SET SESSION
               Session["DataSet"] = dtGrid;

               dtGridDetail = dataset2.Tables[0];
               dtGridDetail .PrimaryKey = new DataColumn[] { dtGrid.Columns["MST_CD;CD"] };
               Session["DataSetDetail"] = dtGridDetail ;
       }
       else
       {
           dtGrid = (DataTable)Session["DataSet"];
           dtGridDetail = (DataTable)Session["DataSetDetail"];
       }

       grid.DataSource = dtGrid;
       grid.DataBind();

       gridDetail.DataSource = dtGridDetail;
       gridDetail.DataBind();
   }

   /// <summary>
   /// Prevent Editing of the Key Column(s) on Cell Updating
   /// </summary>
   /// <param name="sender"></param>
   /// <param name="e"></param>
   protected void grid_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
   {
       ASPxGridView oGrid = sender as ASPxGridView;

       string sSession = string.Empty;

       if ("grid" == oGrid.ClientInstanceName )
       {
           sSession = "DataSet";
       }
       else if ("gridDetail" == oGrid.ClientInstanceName)
       {
           sSession = "DataSetDetail";
       }

       DataTable oDt = (DataTable)Session[sSession];
       
       for (int i = 0; i < oDt.PrimaryKey.Length; i++)
       {
           if (e.Column.FieldName == oDt.PrimaryKey[i].ColumnName)
           {
               e.Editor.ReadOnly = !oGrid.IsNewRowEditing;
               return;
           }
       }
   }

Added By: Quasar Wong at: 5/3/2015 9:00:10 PM    

Hi! I am using Batch Edit mode but CellEditorInitialize event will not fired when clicking New button, any other method can do this scenario else? Thanks!

Added By: Larry (DevExpress Support) at: 5/3/2015 10:55:45 PM    

Hello,

To process your recent post more efficiently, I created a separate ticket on your behalf: T237217: ASPxGridView - Batch Editing - The CellEditorInitialize event will not fired when clicking New button. This ticket is currently in our processing queue. Our team will address it as soon as we have any updates.

Added By: Ravi Anand 1 at: 6/1/2016 12:34:16 AM    Hi, i have applied given example in ASP.NET MVC, folllwing is my gridview settings sample partial code:

[C#]
settings.CellEditorInitialize=(s,e)=>{ASPxGridViewgrid=sasASPxGridView;ASPxEditeditor=(ASPxEdit)e.Editor;if(e.Column.FieldName=="columnName1"||e.Column.FieldName=="columnName2"){e.Editor.ReadOnly=!grid.IsNewRowEditing;}};

now i can't edit while updating existing row data, that seems perfect.

but i can't edit both columns while creating new row using new button. please help me out here. Thank you in advance.

Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>