Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Код для копипаста с экселя в грид
- void grid_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;
- foreach (string line in lines)
- {
- if (row < dataGridView1.RowCount && line.Length > 0)
- {
- 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
- {
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment