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


Viewing all articles
Browse latest Browse all 7205

Trending Articles



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