How to avoid Removing a record when press delete key

8 posts, 1 answers
  1. Somnath
    Somnath avatar
    53 posts
    Member since:
    Jan 2011

    Posted 18 Jan 2011 Link to this post

    Hi,

    Please Suggest any way to avoid removing  record from gridview when press delete key;;;
    Because, already  I have Delete Button to delete the record from gridview......
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 18 Jan 2011 Link to this post

    Hello,

    this will allow you to delete rows programatically (via your button) but not if the user tries to delete via delete key or default context menu.
    this.radGridView1.AllowDeleteRow = true;
    this.radGridView1.UserDeletingRow += new GridViewRowCancelEventHandler(this.radGridView1_UserDeletingRow);

    private void radGridView1_UserDeletingRow(Object sender, GridViewRowCancelEventArgs e)
    {
        e.Cancel = true;
    }

    Hope that helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 18 Jan 2011 Link to this post

    Apologies.. this should actually be enough if you have a button that programatically deletes the rows

    this.radGridView1.AllowDeleteRow = false;

    Richard
  5. Somnath
    Somnath avatar
    53 posts
    Member since:
    Jan 2011

    Posted 18 Jan 2011 Link to this post

    Thanks Richard....
    This code works better....
  6. Eric
    Eric avatar
    24 posts
    Member since:
    Jul 2011

    Posted 19 Mar 2012 Link to this post

    Unfortunately this solution does not work with control x (ctrl-x).  As a side note I was expecting ...

    gridView.MasterTemplate.AllowDeleteRow = False

    ... to just work.  I know, I'm too simple :).


    Basically, when I use ctrl-x, I don't get the event UserDeletingRow fired, so I can't cancel it.  We've also tried capturing keypress/keydown and cancelling the event, but nothing is seeming to stop the grid from deleting the row.  We have an unbound grid.

    So any other suggestions?
  7. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 20 Mar 2012 Link to this post

    Hi Eric,

    Thank you for writing.

    The Ctrl+X, Ctrl+C and Ctrl+V commands are a part of the copy/paste functionality of the grid. You can disable this functionality by setting the CopyClipboardMode property:
    radGridView1.ClipboardCopyMode = Telerik.WinControls.UI.GridViewClipboardCopyMode.Disable;

    I hope that you will find this information useful.
     
    All the best,
    Stefan
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  8. Eric
    Eric avatar
    24 posts
    Member since:
    Jul 2011

    Posted 20 Mar 2012 Link to this post

    Thanks Stefan. It is a bit odd that Telerik provides no way to intercept the Ctrl X in a way that allow us to cancel the row.  I would imagine we are not the developers who would like the ability to throw up a confirmation dialog when a user was about to delete a critical piece of data.  Or simply to allow copy but not delete and/or paste.
  9. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 23 Mar 2012 Link to this post

    Hello Eric,

    Thank you for your suggestion.

    Currently, you can stop any of the commands by disabling the whole functionality in the PreviewKeyDown event and the re-enable it in the KeyUp event of RadGridView:
    void radGridView1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Modifiers == Keys.Control && e.KeyCode == Keys.X)
        {
            radGridView1.ClipboardCopyMode = Telerik.WinControls.UI.GridViewClipboardCopyMode.EnableWithoutHeaderText;
        }
    }
     
    void radGridView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (e.Modifiers == Keys.Control && e.KeyCode == Keys.X)
        {
            radGridView1.ClipboardCopyMode = Telerik.WinControls.UI.GridViewClipboardCopyMode.Disable;
        }
    }

    However, your suggestion seems quite reasonable and we will consider improving the functionality with ability to determine which commands are enabled/disabled. I am adding a PITS item for this request. Please follow this link, where you can add your vote for it:http://www.telerik.com/support/pits.aspx#/public/winforms/10420.

    I have updated your Telerik points for this suggestion. 

    Let us know if you have any other questions or suggestions.

    Regards,
    Stefan
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready