DevExpress GridControl实现右键弹出菜单复制单元格
作者:C/S框架网|www.cscode.ne  发布日期:2019-11-08 09:26:38
  DevExpress GridControl实现右键弹出菜单复制单元格


在单元格点右键【复制单元格】:


贴图图片-微信截图_20191108091740



解决方案:

1. 在界面添加一个ContextMenuStrip组件,如ContextMenuStrip1
2. GridControl.ContextMenuStrip属性设置为ContextMenuStrip1

贴图图片-微信截图_20191108091916


贴图图片-微信截图_20191108091934


复制单元格Click事件:

C# Code:

private void 复制单元格ToolStripMenuItem_Click(object sender, EventArgs e)
{
   if (gvDetail.RowCount > 0 && gvDetail.FocusedColumn != null && gvDetail.FocusedRowHandle >= 0)
   {
      string data = gvDetail.GetFocusedRowCellDisplayText(gvDetail.FocusedColumn);
      if (!String.IsNullOrEmpty(data))
      Clipboard.SetText(data, TextDataFormat.Text);
   }
}

//来源:C/S框架网(www.csframework.com) QQ:23404761



弹出菜单Opening事件可控制菜单项以及是否弹窗菜单:


C# Code:

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
   e.Cancel = !this.IsAddOrEditMode;
   
   if (this.IsAddOrEditMode)
   {
      DataRow row = gvDetail.GetFocusedDataRow();
      if (row == null || gvDetail.FocusedColumn == null)
      {
         e.Cancel = true;
         return;
      }
      
      复制单元格ToolStripMenuItem.Enabled = row != null; } }
      


上一篇 下一篇