Copy and paste into a DataGridView cell (C#)

 ̄綄美尐妖づ 提交于 2019-12-01 10:11:23

问题


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.


回答1:


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



回答2:


You should attach an eventhandler to the cell clicked event and replace the text in the cell by the data in Clipboard.GetText().




回答3:


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

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