Exemplo n.º 1
0
        private void DGV_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ContextMenu mStaged = new ContextMenu();

                mStaged.MenuItems.Add(new MenuItem("Delete Selected Rows", (o, ev) =>
                {
                    if (DGV.SelectedRows.Count > 0)
                    {
                        foreach (DataGridViewRow item in DGV.SelectedRows)
                        {
                            DGV.Rows.RemoveAt(item.Index);
                        }
                    }
                    else
                    {  //optional
                        MessageBox.Show("Please select a row");
                    }
                }));

                mStaged.MenuItems.Add(new MenuItem("Copy Selected Rows", (o, ev) =>
                {
                    if (DGV.GetCellCount(DataGridViewElementStates.Selected) > 0)
                    {
                        try
                        {
                            Clipboard.SetDataObject(
                                DGV.GetClipboardContent());
                        }
                        catch (System.Runtime.InteropServices.ExternalException)
                        {
                            // "The Clipboard could not be accessed. Please try again.";
                        }
                    }
                }));

                mStaged.Show(DGV, new Point(e.X, e.Y));
            }
        }