--先給GridView控件注冊鼠標按下事件
gv.MouseDown += new System.Windows.Forms.MouseEventHandler(this.gv_MouseDown); --在鼠標按下事件里面增加鼠標右鍵判斷,並增加鼠標右鍵菜單復制單元格功能。 private void gv_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { GridHitInfo gridHitInfo = new GridHitInfo(); Point point = new Point(e.X, e.Y); gridHitInfo = (sender as GridView).CalcHitInfo(point); if (gridHitInfo.Column == null) return;
--獲取單元格內容 string copiedData = (sender as GridView).GetRowCellDisplayText(gridHitInfo.RowHandle, gridHitInfo.Column); if (string.IsNullOrEmpty(copiedData)) return; --復制到剪貼板 Clipboard.Clear(); Clipboard.SetData(DataFormats.Text, copiedData); } }