I need to be able to copy a name or names from one application (using the normal copy commands) and then be able to double click the text cell in a DataGridView to paste the data into the grid cell. Any ideas on how to accomplish this? I am attempting to minimize keyboard use for this functionality.
This is actually easier than you might expect.
Create a CellDoubleClick event in your DataGridView and in it put code like this:
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Clipboard.GetText();
}
You should attach an eventhandler to the cell clicked event and replace the text in the cell by the data in Clipboard.GetText()
.
luis_green
I've written this to copy a generic:
DataGridViewSelectedRowCollection dtSeleccionados = dataGrid.SelectedRows;
DataGridViewCellCollection dtCells;
String row;
String strCopiado = "";
for (int i = dtSeleccionados.Count - 1; i >= 0; i--)
{
dtCells = dtSeleccionados[i].Cells;
row = "";
for (int j = 0; j < dtCells.Count; j++)
{
row = row + dtCells[j].Value.ToString() + (((j + 1) == dtCells.Count) ? "" : "\t");
}
strCopiado = strCopiado + row + "\n";
}
try
{
Clipboard.SetText(strCopiado);
}
catch (ArgumentNullException ex)
{
Console.Write(ex.ToString());
}
来源:https://stackoverflow.com/questions/1527728/copy-and-paste-into-a-datagridview-cell-c