C # implements copy and paste of DataGridView control under WinForm

Source: Internet
Author: User

The DataGridView control should be one of the most commonly used controls for database applications, and its convenience is self-explanatory. The user is often asked to " copy data from DataGridView space or paste data into a DataGridView control " in the course of use, The following one of my methods to achieve this request, I hope that we can come up with a better way. Note: Pasting This method only pastes the data from the Excel or Word table to the DataGridView control, and the others have not been tested for the time being.

"Copying data from DataGridView controls"
method is relatively simple, the control has provided us with the Clipboard environment, the call can be, the code is as follows:
The above code can execute COPYwhen typing "CTRL + C" on the DataGridView control;

public void Datagridviewenablecopy (DataGridView p_data)
{
Clipboard.setdata (Dataformats.text, P_data.getclipboardcontent ());
}

  

Paste data into the DataGridView control
This process will be more complicated than the above: get the copied content from the Clipboard environment, assuming it is tabular data, so it is divided into several rows, then each line is divided into several fields, and the DataGridView.Row.Add () method is executed to add the record. The code is as follows:

Public void Datagirdviewcellpaste (DataGridView p_data)
        {
Try
            {
//Get the contents of the Clipboard and split by row
string pastetext = Clipboard.gettext ();
if (string. IsNullOrEmpty (pastetext))
return;
string [] lines = pastetext.split (new char [] {","});
foreach (string line in lines)
                {
if (string. IsNullOrEmpty (line. Trim ()))
continue;
//press TAB to split data
string [] Vals = line. Split (');
P_Data.Rows.Add (vals);
                }
            }
Catch
            {
//Not processed
            }
        }

The next question is when to invoke the above code, you can create a right-click menu for DataGridView, and here I show a call when I type "Ctrl + V" by the judgment. The code is as follows:

Public void Datagridviewenablepaste (DataGridView p_data)
          {
if (p_data = = null)
return;
P_data.keydown + = new Keyeventhandler (p_data_keydown);
          }

Public void P_data_keydown (object sender, KeyEventArgs e)
          {
if (Control.modifierkeys = = Keys.control && E.keycode = = keys.v)
              {
if (sender! = null && sender. GetType () = = typeof (DataGridView))
//Call the paste code above
Datagirdviewcellpaste ((DataGridView) sender);
              }
           }

C # implements copy and paste of DataGridView control under WinForm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.