C# 要通过鼠标c选择datagridview文本框列文本的部分文本吗
我有三列的DataGridView,所有三列都是DataGridViewTextBoxColumn,现在我想用鼠标选择单元格文本。我尝试了以下方法:C# 要通过鼠标c选择datagridview文本框列文本的部分文本吗,c#,datagridview,C#,Datagridview,我有三列的DataGridView,所有三列都是DataGridViewTextBoxColumn,现在我想用鼠标选择单元格文本。我尝试了以下方法: dgView.ReadOnly = false; private void dgView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (e.Control is TextBox) { (
dgView.ReadOnly = false;
private void dgView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is TextBox)
{
(e.Control as TextBox).ReadOnly = true;
}
}
但是这个技巧不起作用。如果你的
dataGridView
有
属性选择为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);
}
}
}
}
谢谢你的回答,但我想用鼠标复制文本,而不是点击任何菜单。请参阅我回答的更新部分。我感谢你的努力,但我想用鼠标选择文本的一部分。我的意思是我们拖动鼠标并选择文本的一部分。我希望我清楚自己想要实现的目标。我不想点击单元格来复制文本,而是想拖动鼠标来选择文本。谢谢我投票支持你的努力。对不起,我误解了。但在我看来,要实现您试图实现的dataGridView行为是不可能的,除非将grid AllowUserToAddress和AllowUserToEditRows的属性设置为true。
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);
}
}
}
}