Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET clipboard
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 16-Apr-13 6:56am
Edited 16-Apr-13 7:02am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think your problem is here:
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:
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>
  Permalink  
Comments
jessicachen12 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this
 
please refer following link
 
DataGridView-Copy-and-Paste
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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!!
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 245
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 Afzaal Ahmad Zeeshan 120
4 DamithSL 115
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 17 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100