gpt4 book ai didi

c# - 在 FullRowSelect 模式下将 datagridview 单元格内容复制到剪贴板

转载 作者:行者123 更新时间:2023-11-30 13:31:25 25 4
gpt4 key购买 nike

我有一个 winform datagridview 来显示客户详细信息,它有一个上下文菜单。我已将 datagridview 选择模式设置为“FullRowSelect”。我想要的是我想将单击的单元格内容的内容复制到剪贴板。不是整行内容。只是单元格内容。

我使用以下代码在右键单击 datagridview 并选择行时显示上下文菜单。

private void dgvCusList_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex != -1 && e.ColumnIndex != -1)
{
if (e.Button == MouseButtons.Right)
{
DataGridViewCell clickedCell = (sender as DataGridView).Rows[e.RowIndex].Cells[e.ColumnIndex];

this.dgvCusList.CurrentCell = clickedCell;

var relativeMousePosition = dgvCusList.PointToClient(Cursor.Position);

this.cnxtMnuCusResult.Show(dgvCusList, relativeMousePosition);
}
}
}

当我单击上下文菜单中的复制菜单项时,我想将单元格内容复制到剪贴板。请帮我解决这个问题。提前致谢。 :)

最佳答案

如果您将 SelectionMode 属性设置为 FullRowSelect,则 DataGridView 的复制功能将复制整行。将值更改为 CellSelect。设置以下属性以仅复制单元格内容。

dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
dataGridView1.MultiSelect = false;

如果你想保留FullRowSelect选择模式,那么按照下面的操作..

 void contextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "Copy" && dataGridView1.CurrentCell.Value != null)
{
Clipboard.SetDataObject(dataGridView1.CurrentCell.Value.ToString(), false);
}
}

void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right && e.ColumnIndex != -1 && e.RowIndex != -1)
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ContextMenuStrip = contextMenu;
dataGridView1.CurrentCell = myDGV.Rows[e.RowIndex].Cells[e.ColumnIndex];
}
}

关于c# - 在 FullRowSelect 模式下将 datagridview 单元格内容复制到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21527646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com