【问题标题】:How to paste tabular data into the "new row" of a DataGridView with AllowUserToAddRows set true?如何在 AllowUserToAddRows 设置为 true 的情况下将表格数据粘贴到 DataGridView 的“新行”中?
【发布时间】:2013-09-02 19:10:44
【问题描述】:

我们在整个 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 中的人。)

【问题讨论】:

  • 你能上传一个小例子会更容易帮助吗

标签: winforms datagridview


【解决方案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;
                            }
                        }

【讨论】:

  • 设置单元格值,试试这个:cell.Value = newValue; dgv.NotifyCurrentCellDirty(true); dgv.BeginEdit(true); dgv.EndEdit();
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 2022-08-16
    相关资源
    最近更新 更多