zoukankan      html  css  js  c++  java
  • WPF的DataGrid控件从excel里复制数据然后粘贴

    WPF的DataGrid控件不能像winform的DataGridView控件一样,支持值的粘贴。WPF的DataGrid控件本质上是跟数据绑定联系在一起,所以需要进行复制粘贴的操作,可以在wpf里用DataGridView控件。如果想进行DataGrid的复制粘贴,只需要在进行复制粘贴的时候,将剪切板上的数据替换成绑定的数据,同样,插入删除等操作,都是改变绑定数据。如下,是一个粘贴的方法,将剪切板的数据转化为绑定数据。

    [csharp] view plain copy
     
    1. #region ctrl+c粘贴  
    2.         private void DataGirdViewCellPaste()  
    3.         {  
    4.             try  
    5.             {  
    6.                 // 获取剪切板的内容,并按行分割     
    7.                 string pasteText = Clipboard.GetText();  
    8.                 if (string.IsNullOrEmpty(pasteText))  
    9.                     return;  
    10.                 int tnum = 0;//剪贴板列数  
    11.                 int nnum = 0;//剪贴板行数  
    12.                 //获得当前剪贴板内容的行、列数  
    13.                 for (int i = 0; i < pasteText.Length; i++)  
    14.                 {  
    15.                     if (pasteText.Substring(i, 1) == " ")  
    16.                     {  
    17.                         tnum++;  
    18.                     }  
    19.                     if (pasteText.Substring(i, 1) == " ")  
    20.                     {  
    21.                         nnum++;  
    22.                     }  
    23.                 }  
    24.                 Object[,] data;  
    25.                 //粘贴板上的数据来自于EXCEL时,每行末都有 ,在DATAGRIDVIEW内复制时,最后一行末没有   
    26.                 if (pasteText.Substring(pasteText.Length - 1, 1) == " ")  
    27.                 {  
    28.                     nnum = nnum - 1;  
    29.                 }  
    30.                 tnum = tnum / (nnum + 1);  
    31.                 data = new object[nnum + 1, tnum + 1];//定义一个二维数组  
    32.                 String rowstr;  
    33.                 rowstr = "";  
    34.                 //MessageBox.Show(pasteText.IndexOf("B").ToString());  
    35.                 //对数组赋值  
    36.                 for (int i = 0; i < (nnum + 1); i++)  
    37.                 {  
    38.                     for (int colIndex = 0; colIndex < (tnum + 1); colIndex++)  
    39.                     {  
    40.                         //一行中的最后一列  
    41.                         if (colIndex == tnum && pasteText.IndexOf(" ") != -1)  
    42.                         {  
    43.                             rowstr = pasteText.Substring(0, pasteText.IndexOf(" "));  
    44.                         }  
    45.                         //最后一行的最后一列  
    46.                         if (colIndex == tnum && pasteText.IndexOf(" ") == -1)  
    47.                         {  
    48.                             rowstr = pasteText.Substring(0);  
    49.                         }  
    50.                         //其他行列  
    51.                         if (colIndex != tnum)  
    52.                         {  
    53.                             rowstr = pasteText.Substring(0, pasteText.IndexOf(" "));  
    54.                             pasteText = pasteText.Substring(pasteText.IndexOf(" ") + 1);  
    55.                         }  
    56.                         data[i, colIndex] = rowstr;  
    57.                     }  
    58.                     //截取下一行数据  
    59.                     pasteText = pasteText.Substring(pasteText.IndexOf(" ") + 1);  
    60.                 }  
    61.                 //获取获取当前选中单元格所在的行序号  
    62.                 int rowindex = dataGrid.SelectedIndex;  
    63.   
    64.                 List<BoxGriderModel> listBoxGriderModel = new List<BoxGriderModel>();  
    65.                 for (int j = 0; j < (nnum + 1); j++)  
    66.                 {  
    67.                     listBoxGriderModel.Add(new BoxGriderModel(double.Parse(data[j, 0].ToString()), double.Parse(data[j, 1].ToString()), double.Parse(data[j, 2].ToString()), double.Parse(data[j, 3].ToString()),  
    68.                         double.Parse(data[j, 4].ToString()), double.Parse(data[j, 5].ToString()), double.Parse(data[j, 6].ToString())));  
    69.   
    70.                 }  
    71.                 m_model.ListBoxGriderModel = listBoxGriderModel;  
    72.                 this.dataGrid.ItemsSource = m_model.ListBoxGriderModel;  
    73.             }  
    74.             catch  
    75.             {  
    76.                 MessageBox.Show("粘贴区域大小不一致");  
    77.                 return;  
    78.             }  
    79.         }  
    80.         #endregion  
  • 相关阅读:
    Halcon的OCR字符识别算法技巧总结
    Halcon中将16位的图像转化为8位的图像
    Photoshop和Halcon如何锐化彩色图像不伤其颜色
    图像滤波和形态学运算中矩形结构元素的运用
    region、xld有对应的字符串时,将region、xld按照行或列排序的算法实现
    影响形状模板匹配的查找速度的参数分析
    select_region_point和select_region_spatial
    提取线条的lines_color、lines_facet、 lines_gauss算子
    求圆环毛刺凸出高度是否超标的算法实现
    相机拍摄时最重要的三个参数——光圈、快门、ISO
  • 原文地址:https://www.cnblogs.com/sjqq/p/8585655.html
Copyright © 2011-2022 走看看