0

ウィンドウフォームを設定しました。現在、SQL Server db の単一のテーブルにリンクされている単一の DataGridView があります。テーブル内の現在のデータを参照できます。

ユーザーが 1 列のデータを Excel シートから DGV にコピー アンド ペーストできるように設定するにはどうすればよいですか?

Excel で A1 に 'x' があり、A2 に 'y' がある場合、DGV に貼り付けたときに行数を保持する必要があります。つまり、この例では、まだ 2 行を超えています。

Code Projectから以下を適応させようとしました。行で失敗した場合(oCell.Value.ToString() != sCells[i])NullReferenceException was unhandled何が間違っていますか?

    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

これらを参照してください:クリップボードから DataGridView (DataGridView1) にデータを貼り付け (Ctrl+V、Shift+Ins) する方法 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 スレッドがたくさんあり、このトピックに関する別の記事に従って、タスクを実装してください。 DataGridView のコピー貼り
付け

DataGridView で選択した行のセルの内容を取得する
方法: Windows フォーム DataGridView コントロールで選択したセル、行、および列を取得する

この助けを願っています..

于 2012-05-17T11:33:54.787 に答える