//在DataGridView的PreviewKeyDown事件中
private void dataGridView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { if (e.Control && e.KeyCode == Keys.V) // 判断是否按下ctrl+v { Paste(dataGridView1, "", 0, false);//粘贴代码 } }
粘贴,行数和列数不足时,自动添加行数列数。
#region 粘贴 public int Paste(DataGridView dgv, string pasteText, int kind, bool b_cut) { try { if (kind == 0) { pasteText = Clipboard.GetText(); } if (string.IsNullOrEmpty(pasteText)) return -1; int rowNum = 0; int columnNum = 0; //获得当前剪贴板内容的行、列数 for (int i = 0; i < pasteText.Length; i++) { if (pasteText.Substring(i, 1) == "\t") { columnNum++; } if (pasteText.Substring(i, 1) == "\n") { rowNum++; } } Object[,] data; //粘贴板上的数据来自于EXCEL时,每行末都有\n,在DATAGRIDVIEW内复制时,最后一行末没有\n if (pasteText.Substring(pasteText.Length - 1, 1) == "\n") { rowNum = rowNum - 1; } columnNum = columnNum / (rowNum + 1); data = new object[rowNum + 1, columnNum + 1]; String rowStr; //对数组赋值 for (int i = 0; i < (rowNum + 1); i++) { for (int colIndex = 0; colIndex < (columnNum + 1); colIndex++) { rowStr = null; //一行中的最后一列 if (colIndex == columnNum && pasteText.IndexOf("\r") != -1) { rowStr = pasteText.Substring(0, pasteText.IndexOf("\r")); } //最后一行的最后一列 if (colIndex == columnNum && pasteText.IndexOf("\r") == -1) { rowStr = pasteText.Substring(0); } //其他行列 if (colIndex != columnNum) { rowStr = pasteText.Substring(0, pasteText.IndexOf("\t")); pasteText = pasteText.Substring(pasteText.IndexOf("\t") + 1); } if (rowStr == string.Empty) rowStr = null; data[i, colIndex] = rowStr; } //截取下一行数据 pasteText = pasteText.Substring(pasteText.IndexOf("\n") + 1); } /*检测值是否是列头*/ /* //获取当前选中单元格所在的列序号 int columnindex = dgv.CurrentRow.Cells.IndexOf(dgv.CurrentCell); //获取获取当前选中单元格所在的行序号 int rowindex = dgv.CurrentRow.Index;*/ int columnindex = -1, rowindex = -1; int columnindextmp = -1, rowindextmp = -1; if (dgv.SelectedCells.Count != 0) { columnindextmp = dgv.SelectedCells[0].ColumnIndex; rowindextmp = dgv.SelectedCells[0].RowIndex; } //取到最左上角的 单元格编号 foreach (DataGridViewCell cell in dgv.SelectedCells) { //dgv.Rows[cell.RowIndex].Selected = true; columnindex = cell.ColumnIndex; if (columnindex > columnindextmp) { //交换 columnindex = columnindextmp; } else columnindextmp = columnindex; rowindex = cell.RowIndex; if (rowindex > rowindextmp) { rowindex = rowindextmp; rowindextmp = rowindex; } else rowindextmp = rowindex; } if (kind == -1) { columnindex = 0; rowindex = 0; } //如果行数超过当前列表行数 if (rowindex + rowNum + 1 > dgv.RowCount) { int mm = rowNum + rowindex + 1 - dgv.RowCount; for (int ii = 0; ii < mm + 1; ii++) { dgv.DataBindings.Clear(); DataRow row = row = ds.Tables[0].NewRow(); ds.Tables[0].Rows.InsertAt(row, ii + rowindex + 1); } } //如果列数超过当前列表列数 if (columnindex + columnNum + 1 > dgv.ColumnCount) { int mmm = columnNum + columnindex + 1 - dgv.ColumnCount; for (int iii = 0; iii < mmm; iii++) { dgv.DataBindings.Clear(); DataGridViewTextBoxColumn colum = new DataGridViewTextBoxColumn(); dgv.Columns.Insert(columnindex + 1, colum); } } //增加超过的行列 for (int j = 0; j < (rowNum + 1); j++) { for (int colIndex = 0; colIndex < (columnNum + 1); colIndex++) { if (colIndex + columnindex < dgv.Columns.Count) { if (dgv.Columns[colIndex + columnindex].CellType.Name == "DataGridViewTextBoxCell") { if (dgv.Rows[j + rowindex].Cells[colIndex + columnindex].ReadOnly == false) { dgv.Rows[j + rowindex].Cells[colIndex + columnindex].Value = data[j, colIndex]; dgv.Rows[j + rowindex].Cells[colIndex + columnindex].Selected = true; } } } } } //清空剪切板内容 if (b_cut) Clipboard.Clear(); return 1; } catch { return -1; } } #endregion