2

我们在整个 UI 中使用 DataGridViews...大多数数据绑定到某个后备表...其中大多数通过 BindingSource。

它们都支持 Ctrl+C 将表格数据复制出来(因此您可以粘贴到 Excel 或 Word 表格等中)。这很简单……它是内置的……只需将 MultiSelect true 和 ClipboardCopyMode 设置为 EnableWithAutoHeaderText(或其他 Enable... 设置之一)。

我们的用户要求我们同样支持 Ctrl+V 来粘贴从 Excel 等复制的表格数据。不幸的是,这并不容易。我创建了一个通用实用程序方法 PasteStringTable(DataGridView),它将表格数据从剪贴板中拉出,然后将其粘贴到参数 DataGridView 的选定单元格中。除了一个关键问题外,我有这个工作:

如果 DataGridView 的 AllowUserToAddRows 设置为 true,并且用户位于底部那个神奇的“新行”中,那么我想为剪贴板上的每一行添加行。但问题是,我以编程方式所做的任何事情似乎都无法让它实际添加该行......我已经尝试了我能想到的一切(或通过 Bing 或 Google 找到):

(1) 我尝试在 DataGridView 上执行 BeginEdit / EndEdit。

(2) 我尝试先将 DataGridView.CurrentCell 设置为我要编辑的单元格,然后是 DataGridView.BeginEdit(true),然后通过设置其 Value 属性来编辑单元格,然后是 DataGridView.EndEdit()。

(3) 我尝试做 #2,但我做了 SendKeys.Send(value) 而不是 .Value = value。

(4) 我尝试在上面的中间做 DataGridView.NotifyCurrentCellDirty(true)。

(5) 我尝试先通过手动执行 DataGridView.Rows.Insert(index, 1) 来修改底层数据源,以尝试在粘贴新行之前强制插入新行……但这会引发异常,因为我的 DataGridViews 是数据-边界。(注意:我不想硬编码对底层数据的特定访问,因为我希望它像粘贴一样工作,其中复制的数据与 DataGridView 的列在视觉上对齐。)

(6) 许多其他变体...

最后,大多数尝试都会导致相同的行为:您可以看到底行被修改......您可以看到您复制的数据的第一行显示在那个神奇的“新行”中,直到您点击该行和该行再次变为空白......除非您手动编辑它,否则它永远不会添加到基础数据中。即使我尝试执行 SendKeys.SendWait(...),它仍然不像我输入该数据时那样。

如果我输入粘贴的数据,我如何以编程方式让那个神奇的“新行”表现得像它一样?

(当然,我不是第一个想要将表格数据粘贴到 DataGridViews 中的人。)

4

2 回答 2

1

经过多次试验和错误,这是我想出的模式,在大多数情况下都很好用。不过,这纯粹是猜测,所以如果有人知道它应该如何工作,请告诉我们。无论如何,这就是我的工作:

                        DataGridViewCell cell = dgv[colIndex, rowIndex];
                        if (!cell.ReadOnly) // && (evenIfNotSelected || cell.Selected)) // Selected is not stable in some cases
                        {
                            if (!cell.OwningRow.IsNewRow)
                                cell.Value = cellData;
                            else
                            {
                                dgv.CurrentCell = cell;
                                dgv.BeginEdit(true);
                                dgv.NotifyCurrentCellDirty(true);
                                //SendKeys.SendWait(cellData);
                                cell.Value = cellData;
                                //dgv.CommitEdit(???);
                                dgv.EndEdit();
                                //PSMUtility.DoMessages();
                                // Do it again to try to overwrite the default value that might get injected by EndEdit adding the row
                                cell.Value = cellData;
                            }
                        }
于 2013-09-04T01:40:20.047 回答
1

我也尝试了很多解决方案,最后它在我的情况下有效。

private void calculationGrid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == 22 && Control.ModifierKeys == Keys.Control)
            {
                calculationGrid.CurrentCell.Value = Clipboard.GetText().Replace("\r", "").Replace("\n", "").Trim();                      
                calculationGrid.BeginEdit(true);
                calculationGrid.NotifyCurrentCellDirty(true);
                calculationGrid.EndEdit();
            }
        }
于 2018-11-24T10:51:33.223 回答