Санкции не работают
4 недели назад
- В свойствах формы прописать свойство keypreview равным true
- В KeyUp добавить следующий обработчик событий:
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))
{
char[] rowSplitter = { '\r', '\n' };
char[] columnSplitter = { '\t' };
//get the text from clipboard
IDataObject dataInClipboard = Clipboard.GetDataObject();
string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.Text);
//split it into lines
string[] rowsInClipboard = stringInClipboard.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries);
//get the row and column of selected cell in grid
int r = grid.SelectedCells[0].RowIndex;
int c = grid.SelectedCells[0].ColumnIndex;
//add rows into grid to fit clipboard lines
if (grid.Rows.Count < (r + rowsInClipboard.Length))
grid.Rows.Add(r + rowsInClipboard.Length - grid.Rows.Count);
// loop through the lines, split them into cells and place the values in the corresponding cell.
for (int iRow = 0; iRow < rowsInClipboard.Length; iRow++)
{
//split row into cell values
string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter);
//cycle through cell values
for (int iCol = 0; iCol < valuesInRow.Length; iCol++)
{
//assign cell value, only if it within columns of the grid
if (grid.ColumnCount - 1 >= c + iCol)
grid.Rows[r + iRow].Cells[c + iCol].Value = valuesInRow[iCol];
}
}
}
}