我有一个DataGridView,它包含必须是ReadOnly的列。问题是,这个值是不可选择的.我只需要让鼠标复制和粘贴成为可能。
此外,DataGridView.SelectionMode必须是DataGridViewSelectionMode.FullRowSelect或DataGridViewSelectionMode.RowHeaderSelect
有什么办法解决这个问题吗?
我搜索了一些属性,比如可编辑的或类似的属性,但我只找到了ReadOnly属性。
编辑:
我只需要ReadOnly单元格中的单元格值.
发布于 2017-03-07 10:59:05
在这段代码中,我以编程的方式创建了这些列,并将第一列设置为只读。使用CellSelect的选择模式,您可以最容易地复制只读数据。如果使用FullRowSelect,则始终复制整行(除非进入编辑模式并复制可编辑单元格)。
dataGridView.Columns.Add( "column1Column", "T1" );
dataGridView.Columns[0].ReadOnly = true;
//The first column (T1) is now ReadOnly
dataGridView.Columns.Add("column2Column", "T2");
dataGridView.Columns.Add("column3Column", "T3");
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
//Or use this if you want to copy cell content of readonly cells
dataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;一种只使用鼠标从ReadOnly单元格中获取数据的简单方法(在我的经验中是用户友好的)是创建一个CellMouseClick事件处理程序。
示例
private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if ( e.Button == MouseButtons.Right )
{
//Set text to clipboard
Clipboard.SetText( dataGridView[e.ColumnIndex, e.RowIndex].Value.ToString() );
}
}发布于 2017-03-07 12:14:52
使用DataGridViewSelectionMode.FullRowSelect获取单击的单元格
DataGridViewCell clickedCell;
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
if (e.Button == MouseButtons.Right)
{
dataGridView1.ClearSelection();
clickedCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
clickedCell.Selected = true;
var cellRectangle = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
// Show context menu with 'Copy' option
contextMenuStrip1.Show(dataGridView1, cellRectangle.Left + e.X, cellRectangle.Top + e.Y);
}
}
catch (Exception ex)
{
throw ex;
}
}然后向表单中添加一个contextMenuStrip,并在copy项中单击event (上下文菜单将从上面的事件中显示):
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
Clipboard.SetText(clickedCell.Value.ToString());
}https://stackoverflow.com/questions/42646172
复制相似问题