zoukankan      html  css  js  c++  java
  • WinForm--DataGridView复制单元格数据

     编写成右键事件:

    1  private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
    2  {
    3     var cellText = this.dataGridView1.CurrentCell.Value == null ? "" : this.dataGridView1.CurrentCell.Value.ToString();
    4     Clipboard.SetDataObject(cellText);
    5  }

    使用快捷键Ctrl+C:

    1  private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
    2 {
    3     if (e.Modifiers.CompareTo(Keys.Control) == 0 && e.KeyCode == Keys.C)
    4     {
    5          Clipboard.SetDataObject(this.dataGridView1.CurrentCell.Value.ToString());
    6     }
    7 }

    使用Clipboard.SetText()向剪贴板写入字符串时,偶尔会引发System.Runtime.InteropServices.ExternalException异常,异常信息如下:

    说明: 由于未经处理的异常,进程终止。 异常信息: System.Runtime.InteropServices.ExternalException 在 System.Windows.Forms.Clipboard.ThrowIfFailed(Int32) 在 System.Windows.Forms.Clipboard.SetDataObject(System.Object, Boolean, Int32, Int32) 在 System.Windows.Forms.Clipboard.SetText(System.String, System.Windows.Forms.TextDataFormat) 在 System.Windows.Forms.Clipboard.SetText(System.String)

    由于剪贴板是系统的公共资源,当有多个程序同时访问时,会引发异常。

    解决方案:

    可以使用Clipboard.SetDataObject()方法代替Clipboard.SetText(),并设置重试次数与重试间隔:

    Clipboard.SetDataObject(text, true, 10, 200);

    原文链接:https://www.cnblogs.com/weca/p/10789801.html

     

  • 相关阅读:
    animation
    0201 ---背景 tableview
    0129 ---稳定定的 plist介绍
    0127 userdefault
    0127 数据库 我的专家
    0122 ---清理缓存
    0122 清楚缓存
    0122---screach
    0121 --view 可以当作线
    0119吧 iPhone 屏幕尺寸
  • 原文地址:https://www.cnblogs.com/zhaoyl9/p/12874217.html
Copyright © 2011-2022 走看看