1

I have a gridview that can have records loaded from an XML file. Now when this list gets loaded into the gridview, there might be some records in there that have a child object say Component inserted as new (that do not exist in our DB, already).

Conditionally, in this gridview the users are not allowed to change any field of the child object Component BUT if we have a newly created child object from the list, we will allow the user to edit certain fields, such as Carrier & Info, of the child object.

My question is how to I allow editor to be shown in the grid for only certain rows, in this case rows with child object as Component.Inserted = true

Saad Khattak
  • 58
  • 1
  • 7

2 Answers2

1

I suggest you go through below DevExpress thread:

How to Conditionally Prevent Editing for Individual Grid Cells

The best approach to make a grid cell read-only based on a condition is to handle the GridView.ShowingEditor event and set the e.Cancel parameter to true when it is necessary to prevent editing.

Example:

// disable editing  
private void gridView1_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e) {  
    GridView view = sender as GridView;  
    if(view.FocusedColumn.FieldName == "Region" && !USCanada(view, view.FocusedRowHandle))  
        e.Cancel = true;  
}

References:
XtraGrid conditional edit

Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
0

I tried to handle ShowingEditor event of the gridview as following :

private void gridViewRecords_ShowingEditor(object sender, CancelEventArgs e)
{
    //gets focused row and casts it into my object
    MyObject mo = (MyObject) gridViewRecords.GetRow(gridViewRecords.FocusedRowHandle);

    //check for my condition
    if(!(mo.Component.Inserted))
    {

          //intended cols, whose editor I want to block
          if(gridViewRecords.FocusedColumnName == "colCarrier"){
               e.Cancel = true;
          }
          else
          if(gridViewRecords.FocusedColumnName == "colInfo"){
               e.Cancel = true;
          }

    }
}
Saad Khattak
  • 58
  • 1
  • 7