【DevExpress】(多行粘贴、块粘贴)

复制是GridControl自带的属性,主要解决的是多个单元格复制的问题,这里涉及到两个参数。

 

主要是粘贴的

先定义两个全局变量,在单元格点击事件的时候获取单元格的行号和列号

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

然后定义个函数,对剪切板的数据进行格式处理

 

  1   private void Paste()
  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  
 16                 int tnum = 0;
 17                 int nnum = 0;
 18                 //获得当前剪贴板内容的行、列数
 19                 for (int i = 0; i < pasteText.Length; i++)
 20                 {
 21                     if (pasteText.Substring(i, 1) == "\t")
 22                     {
 23                         tnum++;
 24                     }
 25                     if (pasteText.Substring(i, 1) == "\n")
 26                     {
 27                         nnum++;
 28                     }
 29                 }
 30                 Object[,] data;
 31                 //粘贴板上的数据来自于EXCEL时,每行末都有\n,在DATAGRIDVIEW内复制时,最后一行末没有\n
 32                 if (pasteText.Substring(pasteText.Length - 1, 1) == "\n")
 33                 {
 34                     nnum = nnum - 1;
 35  
 36                 }
 37  
 38                 tnum = tnum / (nnum + 1);
 39                 data = new object[nnum + 1, tnum + 1];//定义一个二维数组
 40  
 41                 String rowstr;
 42                 rowstr = "";
 43                 //MessageBox.Show(pasteText.IndexOf("B").ToString());
 44                 //对数组赋值
 45                 for (int i = 0; i < (nnum + 1); i++)
 46                 {
 47                     for (int colIndex = 0; colIndex < (tnum + 1); colIndex++)
 48                     {
 49                         //一行中的最后一列
 50                         if (colIndex == tnum && pasteText.IndexOf("\r") != -1)
 51                         {
 52                             rowstr = pasteText.Substring(0, pasteText.IndexOf("\r"));
 53                         }
 54                         //最后一行的最后一列
 55                         if (colIndex == tnum && pasteText.IndexOf("\r") == -1)
 56                         {
 57                             rowstr = pasteText.Substring(0);
 58                         }
 59                         //其他行列
 60                         if (colIndex != tnum)
 61                         {
 62                             rowstr = pasteText.Substring(0, pasteText.IndexOf("\t"));
 63                             pasteText = pasteText.Substring(pasteText.IndexOf("\t") + 1);
 64                         }
 65                         data[i, colIndex] = rowstr;
 66                     }
 67                     //截取下一行数据
 68                     pasteText = pasteText.Substring(pasteText.IndexOf("\n") + 1);
 69  
 70                 }
 71  
 72  
 73  
 74                 for (int j = 0; j < (nnum + 1); j++)
 75                 {
 76                     for (int colIndex = 0; colIndex < (tnum + 1); colIndex++)
 77                     {
 78                          gridView1.SetRowCellValue(j + currentRowIndex, gridView1.Columns[colIndex + currentColumIndex].FieldName, data[j, colIndex]);
 79                         //GV.Rows[j + rowindex].Cells[colIndex + curntindex].Value = data[j, colIndex];
 80                     }
 81                 }
 82  
 83  
 84                 //string[] lines = pasteText.Split(new char[] { ' ', ' ' });
 85                 //string s = String.Join("' '", lines);
 86                 //MessageBox.Show(s);
 87                 //foreach (string line in lines)
 88                 //{
 89                 //    if (string.IsNullOrEmpty(line.Trim()))
 90                 //        continue;
 91                 //    // 按 Tab 分割数据
 92                 //    string[] vals = line.Split(' ');
 93                 //    dt.Rows.Add(vals);
 94  
 95             }
 96             catch
 97             {
 98                 // 不处理
 99             }
100         }

 然后在GridView的KeyDown事件设置粘贴快捷键Ctrl+V

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

 

posted @ 2024-10-18 16:01  陆陆无为而治者  阅读(23)  评论(0编辑  收藏  举报