1. 程式人生 > >【原創】datagridview動態綁定contextmenustrip,並復制單元格數據

【原創】datagridview動態綁定contextmenustrip,並復制單元格數據

event args oar cells ren .get add cell ntc

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,並復制單元格數據