简体   繁体   English

复制并粘贴到DataGridView单元格(C#)

[英]Copy and paste into a DataGridView cell (C#)

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. 我需要能够从一个应用程序中复制一个或多个名称(使用常规的复制命令),然后能够双击DataGridView中的文本单元以将数据粘贴到网格单元中。 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: 在DataGridView中创建一个CellDoubleClick事件,并在其中放入如下代码:

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
   dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Clipboard.GetText();
}

您应该将事件处理程序附加到单元格单击的事件上,并用Clipboard.GetText()的数据替换单元格中的文本。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM