参考连接
    https://blog.csdn.net/qq_38374540/article/details/106192609?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~first_rank_v2~rank_v25-3-106192609.nonecase&utm_term=gridcontrol%20%E7%B2%98%E8%B4%B4

    首先要设置GridControl中的OptionSelection中的Multiselect为true,并且设置MultiSelectMode为cell

    然后设置两个全局变量,用来在RowCellClick事件中指明所点击的cell所在的行数

    1. //获取当前选中单元格所在的列序号
    2. int curntindex;
    3. //获取获取当前选中单元格所在的行序号
    4. int rowindex;
    5. private void SetClickIndex(RowCellClickEventArgs e)
    6. {
    7. //获取当前选中单元格所在的列序号
    8. curntindex = e.Column.ColumnHandle;
    9. //获取获取当前选中单元格所在的行序号
    10. rowindex = e.RowHandle;
    11. }
    12. private void gridView4_RowCellClick(object sender, RowCellClickEventArgs e)
    13. {
    14. SetClickIndex(e);
    15. }

    粘贴Excel的代码

    1. private void Paste(GridView gridView)
    2. {
    3. try
    4. {
    5. GridControl x = (GridControl)gridControl1;
    6. DataTable dt = x.DataSource as DataTable;
    7. // 获取剪切板的内容,并按行分割
    8. string pasteText = Clipboard.GetText();
    9. MessageBox.Show(pasteText);
    10. if (string.IsNullOrEmpty(pasteText))
    11. {
    12. MessageBox.Show("剪切板内容为空,请重新复制!");
    13. return;
    14. }
    15. int tnum = 0;
    16. int nnum = 0;
    17. //获得当前剪贴板内容的行、列数
    18. for (int i = 0; i < pasteText.Length; i++)
    19. {
    20. if (pasteText.Substring(i, 1) == "\t")
    21. {
    22. tnum++;
    23. }
    24. if (pasteText.Substring(i, 1) == "\n")
    25. {
    26. nnum++;
    27. }
    28. }
    29. Object[,] data;
    30. //粘贴板上的数据来自于EXCEL时,每行末都有\n,在DATAGRIDVIEW内复制时,最后一行末没有\n
    31. if (pasteText.Substring(pasteText.Length - 1, 1) == "\n")
    32. {
    33. nnum = nnum - 1;
    34. }
    35. tnum = tnum / (nnum + 1);
    36. data = new object[nnum + 1, tnum + 1];//定义一个二维数组
    37. String rowstr;
    38. rowstr = "";
    39. //MessageBox.Show(pasteText.IndexOf("B").ToString());
    40. //对数组赋值
    41. for (int i = 0; i < (nnum + 1); i++)
    42. {
    43. for (int colIndex = 0; colIndex < (tnum + 1); colIndex++)
    44. {
    45. //一行中的最后一列
    46. if (colIndex == tnum && pasteText.IndexOf("\r") != -1)
    47. {
    48. rowstr = pasteText.Substring(0, pasteText.IndexOf("\r"));
    49. }
    50. //最后一行的最后一列
    51. if (colIndex == tnum && pasteText.IndexOf("\r") == -1)
    52. {
    53. rowstr = pasteText.Substring(0);
    54. }
    55. //其他行列
    56. if (colIndex != tnum)
    57. {
    58. rowstr = pasteText.Substring(0, pasteText.IndexOf("\t"));
    59. pasteText = pasteText.Substring(pasteText.IndexOf("\t") + 1);
    60. }
    61. data[i, colIndex] = rowstr;
    62. }
    63. //截取下一行数据
    64. pasteText = pasteText.Substring(pasteText.IndexOf("\n") + 1);
    65. }
    66. for (int j = 0; j < (nnum + 1); j++)
    67. {
    68. for (int colIndex = 0; colIndex < (tnum + 1); colIndex++)
    69. {
    70. gridView.GetDataRow(j + rowindex)[colIndex + curntindex] = data[j, colIndex];
    71. //GV.Rows[j + rowindex].Cells[colIndex + curntindex].Value = data[j, colIndex];
    72. }
    73. }
    74. //string[] lines = pasteText.Split(new char[] { ' ', ' ' });
    75. //string s = String.Join("' '", lines);
    76. //MessageBox.Show(s);
    77. //foreach (string line in lines)
    78. //{
    79. // if (string.IsNullOrEmpty(line.Trim()))
    80. // continue;
    81. // // 按 Tab 分割数据
    82. // string[] vals = line.Split(' ');
    83. // dt.Rows.Add(vals);
    84. }
    85. catch
    86. {
    87. // 不处理
    88. }
    89. }

    之后便可以在KeyDown事件按下Ctrl+V的时候粘贴上去

    1. private void gridView4_KeyDown(object sender, KeyEventArgs e)
    2. {
    3. GridView gv = (GridView)sender;
    4. if ((e.Control == true) && e.KeyCode == Keys.V)
    5. {
    6. Paste(gv);
    7. }
    8. }