0

我有DataGridView三列,所有三列都是DataGridViewTextBoxColumn,现在我想用鼠标选择单元格文本。我尝试了以下方法:

dgView.ReadOnly = false;
private void dgView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (e.Control is TextBox)
    {
        (e.Control as TextBox).ReadOnly = true;
    }
}

但是这个技巧是行不通的。

4

1 回答 1

2

如果您的 dataGridView 有

 AllowUserToAddRows, AllowUserToEditRows 

属性选择为 false,则只能通过鼠标从单元格中复制该值,使用如下代码:

public void Form1()
{
 //here is code of constructor
 .....
 contextMenuStrip1 = new ContextMenuStrip();
 System.Windows.Forms.ToolStripMenuItem copyStripMenuItem;
 copyStripMenuItem = new ToolStripMenuItem();
 this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
 copyStripMenuItem });
 this.contextMenuStrip1.Name = "contextMenuStrip1";
 this.contextMenuStrip1.Size = new System.Drawing.Size(169, 98);
 copyStripMenuItem.Name = "copyToolStripMenuItem1";
 copyStripMenuItem.Size = new System.Drawing.Size(168, 22);
 copyStripMenuItem.Text = "Copy";
 copyStripMenuItem.Click += new EventHandler(copyStripMenuItem_Click);

 dataGridView1.ContextMenuStrip = contextMenuStrip1;
 ....
 }

 void copyStripMenuItem_Click(object sender, EventArgs e)
 {
    CellCopy();
 }
 public void CellCopy()
 {
    DataGridViewCell cell = dataGridView1.CurrentCell;
    if (cell != null)
    {
       DataGridViewColumn col = dataGridView1.Columns[cell.ColumnIndex];
            if (col is DataGridViewTextBoxColumn)
            {
                if (cell.IsInEditMode)
                {
                    TextBox txt = dataGridView1.EditingControl as TextBox;
                    if (txt != null)
                        txt.Copy();
                }
                else
                {
                    string val = cell.FormattedValue == null ? "" : cell.FormattedValue.ToString();
                    if (val == "")
                        Clipboard.Clear();
                    else
                        Clipboard.SetText(val);
                }
            }
        }
    }

更新

此外,如果您需要通过鼠标单击来复制 dataGridView1 的值,您可以这样编写。

 public void Form1_Load(object sender, EventArgs e)
 {  
  there is some code of constructor here
.....
dataGridView1.CellContentDoubleClick+=new DataGridViewCellEventHandler(dataGridView1_CellContentDoubleClick);
......
 }
  void dataGridView3_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        CellCopy();
    }

     /// <summary>
    /// Copy the current value in the buffer
    /// </summary>
    public void CellCopy()
    {
        DataGridViewCell cell = dataGridView1.CurrentCell;
        if (cell != null)
        {
            DataGridViewColumn col = dataGridView1.Columns[cell.ColumnIndex];
            if (col is DGW_NewCellsColumn)
            {
                if (cell.IsInEditMode)
                {
                    TextBox txt = dataGridView1.EditingControl as TextBox;
                    if (txt != null)
                        txt.Copy();
                }
                else
                {
                    string val = cell.FormattedValue == null ? "" :  cell.FormattedValue.ToString();
                    if (val == "")
                        Clipboard.Clear();
                    else
                        Clipboard.SetText(val);
                }
            }
        }
    }
于 2013-03-23T23:10:40.067 回答