Paste the Excel content into DataGridView, DataGridView paste into Excel

Source: Internet
Author: User
Tags rowcount

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.