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

时间:2009-10-06 19:53:30

标签: c# datagridview copy paste

我需要能够从一个应用程序复制一个或多个名称(使用普通的复制命令),然后能够双击DataGridView中的文本单元格将数据粘贴到网格单元格中。有关如何实现这一目标的任何想法?我正在尝试最小化键盘使用此功能。

3 个答案:

答案 0 :(得分:8)

这实际上比您预期的要容易。

在DataGridView中创建一个CellDoubleClick事件,并在其中放置如下代码:

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

我写这篇文章是为了复制一个通用名称:

        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());
        }
相关问题