c# Dev GridControl实现右键弹出菜单复制单元格

时间:2024-03-22 20:01:11
在单元格点右键【复制单元格】:
 

c# Dev GridControl实现右键弹出菜单复制单元格

 
 
 
解决方案:
 
1. 在界面添加一个ContextMenuStrip组件,如ContextMenuStrip1
2. GridControl.ContextMenuStrip属性设置为ContextMenuStrip1
c# Dev GridControl实现右键弹出菜单复制单元格
 
c# Dev GridControl实现右键弹出菜单复制单元格
 
 
复制单元格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; } }