void Datagridview1_keydown (object sender, KeyEventArgs e) {if (E.control && e.keycode = = KEYS.C ) {DataObject d = datagridview1.getclipboardcontent (); Clipboard.setdataobject (d); E.handled = true; } else if (e.control && e.keycode = = keys.v) {string s = Clipboard.gettext () ; String[] lines = s.split (' \ n '); int row = DataGridView1.CurrentCell.RowIndex; int col = DataGridView1.CurrentCell.ColumnIndex; Check if need add row if ((Datagridview1.rows.count-row) < lines. Length) {DATAGRIDVIEW1.ROWS.ADD (lines). Length-(Datagridview1.rows.count-row)); } foreach (string lines) {if (line. Length > 0) && row < Datagridview1.rowcount) {string[] cells = line. Split (' \ t '); for (int i = 0; i < cells. GetLength (0); ++i) {if (col + i < This.dataGridView1.ColumnCount) {Datagridview1[col + i, row]. Value = Convert.changetype (Cells[i], Datagridview1[col + i, row]. ValueType); } else {break; }} row++; } else if (row = = Datagridview1.rowcount && line. Length > 0) {break; } } } }
Copy and paste content from Excel directly into DataGridView or other table controls (similar to other control code) to paste the contents of the table into Excel.
Implement CTRL + C and CTRL + V. Code as follows:
Paste the Excel content into DataGridView, DataGridView paste into Excel