首页 文章

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

提问于
浏览
1

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

3 回答

  • 1

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

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

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

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

  • 8

    我写这个来复制一个泛型:

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

相关问题