how to disable copy paste function from a GridView c#

I have a GridView that I need to protect from people copying and pasting the data from the grid to another application
LVL 2
Jayesh AcharyaTechnichal ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AliSyedCommented:
<body oncopy="return false;" onpaste="return false;" oncut="return false;">
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
this is a WinForm and I get an error using the code you suggested,

here is how i am initializing my grid

        public void InitializeGrid(DataGridView lv_StockCheckdataGridView
                                  ,string GridViewName)
        {
            <body oncopy="return false;" onpaste="return false;" oncut="return false;">


            lv_StockCheckdataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            lv_StockCheckdataGridView.ContextMenuStrip = this.contextMenuStrip1;
            lv_StockCheckdataGridView.AllowUserToAddRows = false;
            lv_StockCheckdataGridView.AllowUserToDeleteRows = false;
            lv_StockCheckdataGridView.AllowUserToOrderColumns = true;
            lv_StockCheckdataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            lv_StockCheckdataGridView.Size = new System.Drawing.Size(1074, 604);
            lv_StockCheckdataGridView.TabIndex = 5;
            lv_StockCheckdataGridView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGridView1_MouseDown);
            lv_StockCheckdataGridView.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.dataGridView_CellFormatting);
            lv_StockCheckdataGridView.Location = new System.Drawing.Point(12, 43);
            lv_StockCheckdataGridView.Name = GridViewName;
            lv_StockCheckdataGridView.ReadOnly = true;
            lv_StockCheckdataGridView.AllowUserToResizeColumns = true;

        }
trestanCommented:
If this is what you want:

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true && e.KeyCode == Keys.C)
            {
                MessageBox.Show("No copy allowed!");
            }
        }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trestanCommented:
And of course if you want to disable paster:
 
       private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
       {
           if (e.Control == true && e.KeyCode == Keys.V)
           {
               MessageBox.Show("No pasta allowed:)");
           }
        }  
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
thanks worked as expected, but needed to check the warning messages as it had no pasta instead of no paste ... well that brought some smiles during testing ...

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.