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

发布于 2024-08-07 12:34:31 字数 117 浏览 5 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

献世佛 2024-08-14 12:34:32

您应该将事件处理程序附加到单元格单击事件,并用 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().

皓月长歌 2024-08-14 12:34:32

我写这个是为了复制泛型:

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

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());
        }
绳情 2024-08-14 12:34:31

这实际上比您想象的要容易。

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

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

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文