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);
}
}
}
}