Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello everyone!

This is the first time to use clipboard to copy data from Excel and paste it into a gridview.

I wrote code like this:

private void gridControlInsert_KeyPress(object sender, KeyPressEventArgs e)
       {
           if (Convert.ToInt32(e.KeyChar) == 22)
           {
               ////Catch CTRL + V (Paste)
               PasteData(Clipboard.GetText());
           }
       }

       private void PasteData(string pClipboard)
       {
           List<List<string>> table = new List<List<string>>();

           string importText = pClipboard;

           importText = importText.Replace("\n", "");

           string[] lines = importText.Split('\r');
           for (int x = 0; x < lines.Length; x++)
           {
               if (string.IsNullOrEmpty(lines[x]))
               {
                   break;
               }

               List<string> cellsList = new List<string>();
               string[] cells = lines[x].Split('\t');

               cellsList.AddRange(cells);

               table.Add(cellsList);
           }
           try
           {
               gridControlInsert.BeginUpdate();

               DataView dataView = (DataView)gridViewInsertMas.DataSource;

               for (int x = 0; x < table.Count; x++)
               {
                   DataRow row = dataView.Table.NewRow();
                   for (int y = 0; y < table[x].Count; y++)
                   {
                       row[y] = table[x][y];

                   }
                   dataView.Table.Rows.Add(row);
               }
               //string s = dataView.Table.Rows[0].ItemArray[0].ToString();
               //XtraMessageBox.Show(s,"Check",MessageBoxButtons.OK);  The messagebox shows well

               gridControlInsertMas.DataSource = dataView;

           }




My idea is simple:
1. get text from clipboard
2. create a datatable
3. parse the datatable and copy cell by cell into the dataview


My problem comes from the fact that every time when I copy data from Excel, for exemple like this:
999 aaa
888 bbb

It pastes the data into gridview I want but shows

999 aaa(this is a cell) aaa
888 bbb


Then I add a new default row in the gridview, it shows like this:

999 aaa( this is a cell)
999 aaa (2 cells, exactly the same thing in Excel)
888 bbb (2 cells, exactly the same thing in Excel)


I just can't find out where is the problem. I checked the dataView, it seems it is good. I even add MessageBox to see what is in the first cell of dataView, it shows well "999"...



Could anyone help me? Many thanks in advance!!!
Posted
Updated 16-Apr-13 6:02am
v2

I think your problem is here:
XML
List<string> cellsList = new List<string>();
    string[] cells = lines[x].Split('\t');

    cellsList.AddRange(cells);

    table.Add(cellsList);



In order to create the table you should perhaps do something lik:
C#
string[] lines = importText.Split('\r');
          for (int x = 0; x < lines.Length; x++)
                sting[] cell = lines.Split('\t')
                for (int y = 0; x < cell.Length; x++)</pre>
 
Share this answer
 
Comments
jessicachen12 17-Apr-13 5:31am    
thank you, I solved this problem. In fact, it just needs to add "e.Handle" below like this:
PasteData(Clipboard.GetText());
e.Handled = true;

which means I will handle with all enters.
Try this

please refer following link

DataGridView-Copy-and-Paste
 
Share this answer
 
The solution is quite simple,

I just add e.Handle like this :

PasteData(Clipboard.GetText());
e.Handle = true;


this means that all enters with be handled by humain.

In fact, the problem comes from the fact that it not only created table and copy data from the table to gridview, but also handled the "Ctrl + V" which meands copied something or entered the char "V" as well.


Thank you for all your help!!
 
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