Copy and paste into a DataGridView cell (C#)

末鹿安然 提交于 2019-12-01 12:00:29

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());
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!