WPF的DataGrid控件从excel里复制数据然后粘贴
WPF的DataGrid控件不能像winform的DataGridView控件一样,支持值的粘贴。WPF的DataGrid控件本质上是跟数据绑定联系在一起,所以需要进行复制粘贴的操作,可以在wpf里用DataGridView控件。如果想进行DataGrid的复制粘贴,只需要在进行复制粘贴的时候,将剪切板上的数据替换成绑定的数据,同样,插入删除等操作,都是改变绑定数据。如下,是一个粘贴的方法,将剪切板的数据转化为绑定数据。
- #region ctrl+c粘贴
- private void DataGirdViewCellPaste()
- {
- try
- {
- // 获取剪切板的内容,并按行分割
- string pasteText = Clipboard.GetText();
- if (string.IsNullOrEmpty(pasteText))
- return;
- int tnum = 0;//剪贴板列数
- int nnum = 0;//剪贴板行数
- //获得当前剪贴板内容的行、列数
- for (int i = 0; i < pasteText.Length; i++)
- {
- if (pasteText.Substring(i, 1) == "\t")
- {
- tnum++;
- }
- if (pasteText.Substring(i, 1) == "\n")
- {
- nnum++;
- }
- }
- Object[,] data;
- //粘贴板上的数据来自于EXCEL时,每行末都有\n,在DATAGRIDVIEW内复制时,最后一行末没有\n
- if (pasteText.Substring(pasteText.Length - 1, 1) == "\n")
- {
- nnum = nnum - 1;
- }
- tnum = tnum / (nnum + 1);
- data = new object[nnum + 1, tnum + 1];//定义一个二维数组
- String rowstr;
- rowstr = "";
- //MessageBox.Show(pasteText.IndexOf("B").ToString());
- //对数组赋值
- for (int i = 0; i < (nnum + 1); i++)
- {
- for (int colIndex = 0; colIndex < (tnum + 1); colIndex++)
- {
- //一行中的最后一列
- if (colIndex == tnum && pasteText.IndexOf("\r") != -1)
- {
- rowstr = pasteText.Substring(0, pasteText.IndexOf("\r"));
- }
- //最后一行的最后一列
- if (colIndex == tnum && pasteText.IndexOf("\r") == -1)
- {
- rowstr = pasteText.Substring(0);
- }
- //其他行列
- if (colIndex != tnum)
- {
- rowstr = pasteText.Substring(0, pasteText.IndexOf("\t"));
- pasteText = pasteText.Substring(pasteText.IndexOf("\t") + 1);
- }
- data[i, colIndex] = rowstr;
- }
- //截取下一行数据
- pasteText = pasteText.Substring(pasteText.IndexOf("\n") + 1);
- }
- //获取获取当前选中单元格所在的行序号
- int rowindex = dataGrid.SelectedIndex;
- List<BoxGriderModel> listBoxGriderModel = new List<BoxGriderModel>();
- for (int j = 0; j < (nnum + 1); j++)
- {
- 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()),
- double.Parse(data[j, 4].ToString()), double.Parse(data[j, 5].ToString()), double.Parse(data[j, 6].ToString())));
- }
- m_model.ListBoxGriderModel = listBoxGriderModel;
- this.dataGrid.ItemsSource = m_model.ListBoxGriderModel;
- }
- catch
- {
- MessageBox.Show("粘贴区域大小不一致");
- return;
- }
- }
- #endregion
WPF的DataGrid控件从excel里复制数据然后粘贴的更多相关文章
- WPF 4 DataGrid 控件(进阶篇一)
原文:WPF 4 DataGrid 控件(进阶篇一) 上一篇<WPF 4 DataGrid 控件(自定义样式篇)>中,我们掌握了DataGrid 列表头.行表头.行.单元格相关的 ...
- WPF 4 DataGrid 控件(进阶篇二)
原文:WPF 4 DataGrid 控件(进阶篇二) 上一篇<WPF 4 DataGrid 控件(进阶篇一)>中我们通过DataGridTemplateColumn 类自定义编辑 ...
- WPF 4 DataGrid 控件(基本功能篇)
原文:WPF 4 DataGrid 控件(基本功能篇) 提到DataGrid 不管是网页还是应用程序开发都会频繁使用.通过它我们可以灵活的在行与列间显示各种数据.本篇将详细介绍WPF 4 中 ...
- WPF 4 DataGrid 控件(自定义样式篇)
原文:WPF 4 DataGrid 控件(自定义样式篇) 在<WPF 4 DataGrid 控件(基本功能篇)>中我们已经学习了DataGrid 的基本功能及使用方法.本篇将继续 ...
- WPF中DataGrid控件内Button的Command和CommandParameter的绑定
场景:视频上传功能,上传列表使用DataGrid控件,视频有不同的状态对应不同的操作,DataGrid中最后一列为操作列,里面是Button控件.希望点击Button后执行对应的操作,但是设置Butt ...
- WPF中DataGrid控件的过滤(Filter)性能分析及优化
DataGrid控件是一个列表控件, 可以进行过滤,排序等.本文主要针对DataGrid的过滤功能进行分析, 并提供优化方案. 1)DataGrid的过滤过程: 用户输入过滤条件 ...
- WPF 自定义DataGrid控件样式
内容转自https://www.cnblogs.com/xiaogangqq123/archive/2012/05/07/2487166.html 一.DataGrid基本样式(一) 小刚已经把Dat ...
- 关于使用MVVM模式在WPF的DataGrid控件中实现ComboBox编辑列
最近在做一个组态软件的项目,有一个需求需要在建立IO设备变量的时候选择变量的类型等. 建立IO变量的界面是一个DataGrid实现的,可以一行一行的新建变量,如下如所示: 这里需要使用带有ComboB ...
- Working Experience - WPF 中 DataGrid 控件的应用
问题: 添加控件后, 编辑单元格会出现异常 绑定 ItemsSource 属性后, 更新绑定对象的数据, UI 不刷新 如何显示控件中 ComboBox 类型 解决方法: 绑定 ItemsSource ...
随机推荐
- 揭秘ThreadLocal(转)
转载自:掘金大闲人柴毛毛博客. ThreadLocal是开发中最常用的技术之一,也是面试重要的考点.本文将由浅入深,介绍ThreadLocal的使用方式.实现原理.内存泄漏问题以及使用场景. Thre ...
- AI的分支学科
AI 的分支学科 [References]AAI(Advanced Artificial Intelligence)
- 腾讯QQ的发展与未来
http://wenku.baidu.com/view/15166ddfc1c708a1284a447d.html 腾讯QQ的发展与未来
- IHttpHandler的那些事
写在前面 从上家公司离职,在家休息,闲着无聊,觉得还是有必要将IHttpHanlder的内容,做一个总结.发现在写demo的过程中,总觉得有点生疏了,项目中很少使用自定义的类来实现该接口.当然,一般处 ...
- 解决Enter键与input 、a标签触发的事件的冲突
无论是 <button type="button" onclick="console.log('123');">123</button> ...
- Mysql 创建表和删除表
在数据库中创建一张表的基本语法如下: CREATE TABLE tablename (column_name_1 column_type_1 constraints, column_name_2 co ...
- Unity和安卓互调
Unity调安卓 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); And ...
- cnblogs博客迁移到hexo
cnblogs博客备份 备份地址:https://i.cnblogs.com/BlogBackup.aspx?type=1 备份文件为xml格式,打开备份文件,如下所示: <?xml versi ...
- Lucene:基于Java的全文检索引擎简介 (zhuan)
http://www.chedong.com/tech/lucene.html ********************************************** Lucene是一个基于Ja ...
- dependent-name ‘xxx::yyy’ is parsed as a non-type, but instantiation yields a type
简言之,就是说你该用typename的地方没用typename,如以下代码 template<class Cont> void frontInsertion(Cont& ci) { ...