从excel复制数据并将其粘贴到datagridview后,只粘贴空值。帮我。 [英] After copying the data from excel and pasting it into the datagridview, only the empty value is pasted. Help me.

查看:83
本文介绍了从excel复制数据并将其粘贴到datagridview后,只粘贴空值。帮我。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private string ClipboardData
       {
           get
           {
               IDataObject iData = Clipboard.GetDataObject();
               if (iData == null) return '';

               if (iData.GetDataPresent(DataFormats.Text))
                   return (string)iData.GetData(DataFormats.Text);
               return '';
           }
           set { Clipboard.SetDataObject(value); }
       }

       private void CopyRowAdd(string data, int idx)
       {
           try
           {
               DataTable tblData = (DataTable)grdXYMappingInfo.DataSource;
               DataRow newRow = tblData.NewRow();

               string[] rawRowData = data.Split(new char[] { '\r', '\x09' });
               string[] RowData = new string[tblData.Columns.Count];

               for (int i = 0; i <  RowData.Length; i++)
               {
                   if (i >= tblData.Columns.Count) break;
                   else
                   {
                       newRow[i] = RowData[i];
                   }
               }
               tblData.Rows.InsertAt(newRow, grvXYMappingInfo.FocusedRowHandle + idx);
           }
           catch (Exception x)
           {
               System.Console.WriteLine(x.Message);
           }
       }

       private void grvXYMappingInfo_KeyDown(object sender, KeyEventArgs e)
       {

           if (e.Control && e.KeyCode == Keys.C)
           {

               grvXYMappingInfo.CopyToClipboard();

           }
           else if (e.Control && e.KeyCode == Keys.V)
           {
               try
               {

                   string[] selectedRow = ClipboardData.Split('\n');
                   string[] addRow = new string[selectedRow.Length - 1];
                   System.Array.Copy(selectedRow, 1, addRow, 0, selectedRow.Length - 1);

                   if (addRow.Length <  1) return;
                   int rowidx = 1;
                   foreach (string row in addRow)
                   {
                       CopyRowAdd(row, rowidx);
                       rowidx++;
                   }
               }
               catch (Exception x)
               {
                   System.Console.WriteLine(x.Message);
               }

           }
       }





什么我试过了:





What I have tried:

After copying the data from Excel and pasting it into the datagridview, only the empty value is pasted. Help me.

推荐答案

该代码甚至不会编译,更不用说粘贴空数据了。

That code won't even compile, much less paste empty data.
private string ClipboardData
       {
           get
           {
               IDataObject iData = Clipboard.GetDataObject();
               if (iData == null) return '';

               if (iData.GetDataPresent(DataFormats.Text))
                   return (string)iData.GetData(DataFormats.Text);
               return '';
           }
           set { Clipboard.SetDataObject(value); }
       }

你的返回''语句都会出错,因为你不能有一个空字符文字 :''

即使您修复了这个问题,也无法从字符串属性返回 char 值 - 这会给您一个错误同样,可能是类型转换消息。

几乎可以肯定,你应该返回一个空字符串,而 null 可能是一个更好的选择。



因此,您测试的代码不是您显示的代码 - 这意味着您正在执行的测试是在代码的最后一个版本上确实编译了,这可能就是为什么它不能按你认为应该的方式运行。



修复它,然后再试一次 - 并使用调试器查找究竟发生了什么!

Will give you an error on both of your return '' statements because you can't have an "empty character literal": ''
Even if you fix that, you can't return a char value from a string property - that will give you an error as well, probably a type conversion message.
Almost certainly, you should be returning an empty string "" instead, though a null might be a better choice.

So the code you are testing isn't the code you are showing - which means that the tests you are doing are on the last version of your code that did compile, and that is probably why it doesn't work the way you think it should.

Fix that, and try again - and use the debugger to find out exactly what is going on!


这个数据是什么?一个细胞?超过一个?什么格式?它是如何进入剪贴板的?为什么不像其他人一样在代码(而不是剪贴板)中执行此操作。你没有创造(额外)中间人。
what is this "data"? One cell? More than one? What format? How did it get in the clipboard? Why don't you do it in code (instead of the clipboard), like everyone else. You created an (extra) "middle-man" for nothing.


这篇关于从excel复制数据并将其粘贴到datagridview后,只粘贴空值。帮我。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆