Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 22-May-19 20:50pm
v3

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); }
       }
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!
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900