Read-only cell (simulation) and the paste functionnality

4 posts, 0 answers
  1. Alain
    Alain avatar
    7 posts
    Member since:
    Oct 2012

    Posted 10 Apr 2015 Link to this post

    Hi, I am using a RadGridView in which some of the cells might be read-only depending on the values of the data in other columns.
    To simulate the read-only behavior, I use «CellBeginEdit» event has recommended in some threads of this forum.
    My code looks like this:

    private void radGridView_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
            {
                e.Cancel = !IsCellUpdatable();      
            }

    The «IsCellUpdatable» function seen above returns true or false depending on if the cell has to be modified.
    Up to that part, everything works properly.

    The problem I encounter is that a user can still paste some data into the cell using the keyboard shortcut «Ctrl-V». The pasting command does not trigger the CellBeginEdit event, so the data is entered in the cell.  Thus the read-only behavior can be bypassed.  So, I need to prevent the pasting from somewhere else.  

    I have tried using the grid's KeyDown event but it is not fired.  I then tried to override the grid's behavior and the ProcessKey event to catch the «Ctrl-V» command but then I do not have access to my form's «IsCellUpdatable» method (it is not static and cannot be). 

    Any other suggestions to trap and prevent the paste command?

    Thanks for your help

  2. Alain
    Alain avatar
    7 posts
    Member since:
    Oct 2012

    Posted 10 Apr 2015 in reply to Alain Link to this post

    Just forgot to mention that I am using Q3 2012 and don't really have the option to upgrade for now... so if the solution could work with this version it would be nice. ;-)
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Mahmoud
    Mahmoud avatar
    10 posts
    Member since:
    Mar 2015

    Posted 13 Apr 2015 in reply to Alain Link to this post

    Hi Alain ,

    e.Row.Cells(e.ColumnIndex).ReadOnly = IsCellUpdatable(); 

    e.Cancel = !IsCellUpdatable();  

  5. Dess
    Admin
    Dess avatar
    1605 posts

    Posted 15 Apr 2015 Link to this post

    Hello,

    Thank you for writing.

    Canceling the CellBeginEdit event is the appropriate place to prevent entering edit mode for a certain cell. However, when you copy and paste, the cell value is changed without entering edit mode at all. In this situation, you can subscribe to the RadGridView.Pasting event and cancel it according to the returned result from the IsCellUpdatable method. However, this approach is applicable for versions newer than Q1 2013 (version 2013.1.220).

    For all prior versions you can override the MasterGridViewTemplate.Paste method and stop the base logic:
    public class CustomGrid : RadGridView
    {
        protected override RadGridViewElement CreateGridViewElement()
        {
            return new CustomRadGridViewElement();
        }
     
        public override string ThemeClassName
        {
            get
            {
                return typeof(RadGridView).FullName;
            }
        }
    }
     
    public class CustomRadGridViewElement : RadGridViewElement
    {
        protected override MasterGridViewTemplate CreateTemplate()
        {
            return new CustomMasterGridViewTemplate();
        }
     
        protected override Type ThemeEffectiveType  
        {
            get 
            {
                return typeof(RadGridViewElement);  
            }
        }
    }
     
    public class CustomMasterGridViewTemplate : MasterGridViewTemplate
    {
        public override void Paste()
        {
            if ("your condition here".Length > 0)
            {
                return;
            }
            base.Paste();
        }
    }

    I hope this information helps. Should you have further questions, I would be glad to help.
     
    Regards,
    Dess
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
Back to Top