首页 > 代码库 > 【原创】datagridview动态绑定contextmenustrip,并复制单元格数据

【原创】datagridview动态绑定contextmenustrip,并复制单元格数据

   public class DGVCopy
    {
        public DataGridView DataGridViewCopy { get; set; }
        public int X { get; set; }
        public int Y { get; set; }
        public void CopyData()
        {
            DataGridViewCopy.CellMouseClick += new DataGridViewCellMouseEventHandler(DataGridViewCopy_CellMouseClick);
        }
        void DataGridViewCopy_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.Button == MouseButtons.Right )
                {
                    DataGridViewCopy.CurrentCell = DataGridViewCopy.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    ContextMenuStrip contextMenuStrip1 = new ContextMenuStrip();
                    contextMenuStrip1.Items.Add("复制");
                    contextMenuStrip1.Size = new Size(30, 30);
                    DataGridViewCopy.ContextMenuStrip = contextMenuStrip1;
                    contextMenuStrip1.Show(X,Y);
                    contextMenuStrip1.Click += new EventHandler(contextMenuStrip1_Click);
                }
            }
        }
        void contextMenuStrip1_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(DataGridViewCopy.GetClipboardContent().GetText().Trim());
        }
    }

【原创】datagridview动态绑定contextmenustrip,并复制单元格数据