0

我已经设置了一个窗体。目前它有一个 DataGridView 链接到 SQL Server 数据库中的单个表。我可以浏览表中的当前数据。

如何进行设置,以便用户可以将 Excel 工作表中的单列数据复制并粘贴到 DGV 中?

如果在 Excel 中,我在 A1 中有“x”,在 A2 中有“y”,那么在粘贴到 DGV 时必须保留行数,即对于本示例,它仍将超过 2 行

我试图从Code Project改编以下内容。if 失败就行 if (oCell.Value.ToString() != sCells[i])with a NullReferenceException was unhandledWhat is I doing wrong? 我在做什么错?

    private void uxChargeBackDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        PasteClipboard();
       //uxChargeBackDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Clipboard.GetText();
    }


    private void PasteClipboard()
    {
        try
        {
            string s = Clipboard.GetText();
            string[] lines = s.Split('\n');
            int iFail = 0, iRow = uxChargeBackDataGridView.CurrentCell.RowIndex;
            int iCol = uxChargeBackDataGridView.CurrentCell.ColumnIndex;
            DataGridViewCell oCell;
            foreach (string line in lines)
            {
                if (iRow < uxChargeBackDataGridView.RowCount && line.Length > 0)
                {
                    string[] sCells = line.Split('\t');
                    for (int i = 0; i < sCells.GetLength(0); ++i)
                    {
                        if (iCol + i < this.uxChargeBackDataGridView.ColumnCount)
                        {
                            oCell = uxChargeBackDataGridView[iCol + i, iRow];
                            if (!oCell.ReadOnly)
                            {
                                if (oCell.Value.ToString() != sCells[i])
                                {
                                    oCell.Value = Convert.ChangeType(sCells[i],
                                                          oCell.ValueType);
                                    oCell.Style.BackColor = Color.Tomato;
                                }
                                else
                                    iFail++;
                                //only traps a fail if the data has changed 
                                //and you are pasting into a read only cell
                            }
                        }
                        else
                        { break; }
                    }
                    iRow++;
                }
                else
                { break; }
                if (iFail > 0)
                    MessageBox.Show(string.Format("{0} updates failed due" +
                                    " to read only column setting", iFail));
            }
        }
        catch (FormatException)
        {
            MessageBox.Show("The data you pasted is in the wrong format for the cell");
            return;
        }
    }  
4

2 回答 2

1

非常简单,可以解决问题:

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

编辑*

如果您想粘贴到多个单元格而不是单个单元格,请查看此博客文章:

http://happysoftware.blogspot.co.uk/2011/07/c-code-snippet-paste-to-datagridview.html

于 2012-05-17T11:01:36.387 回答
0

请参阅这些:如何将数据从剪贴板粘贴(Ctrl+V、Shift+Ins)到 DataGridView(DataGridView1)C#复制并粘贴到 DataGridView 单元格(C#)

private void form1_KeyUp(object sender, KeyEventArgs e)
{
    //if user clicked Shift+Ins or Ctrl+V (paste from clipboard)
    if ((e.Shift && e.KeyCode == Keys.Insert) || (e.Control && e.KeyCode == Keys.V))
    {
       /// your paste copy/paste code here
    }
}

有很多参考 SO Thread 和关于此主题的另一篇文章遵循这些来实现您的任务.. DataGridView 复制和粘贴
复制粘贴在 Datagridview 控件中
DataGridView DataBound 复制、粘贴、拖动、拖放
行复制/粘贴功能在 DataGridview(Windows 应用程序)

获取 DataGridView 中选定行的单元格内容
如何:获取 Windows 窗体 DataGridView 控件中选定的单元格、行和列

希望这有帮助..

于 2012-05-17T11:33:54.787 回答