This is a migrated thread and some comments may be shown as answers.

Read-only cell (simulation) and the paste functionnality

3 Answers 67 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alain
Top achievements
Rank 1
Alain asked on 10 Apr 2015, 03:21 PM

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

3 Answers, 1 is accepted

Sort by
0
Alain
Top achievements
Rank 1
answered on 10 Apr 2015, 03:25 PM
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. ;-)
0
Mahmoud
Top achievements
Rank 1
answered on 13 Apr 2015, 08:30 AM

Hi Alain ,

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

e.Cancel = !IsCellUpdatable();  

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 15 Apr 2015, 12:06 PM
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.

 
Tags
GridView
Asked by
Alain
Top achievements
Rank 1
Answers by
Alain
Top achievements
Rank 1
Mahmoud
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or