DataGridView copy paste rows with cell contents

Source: Internet
Author: User

Recently encountered a question about the DataGridView shortcut key copy, paste, cut, delete, and other operations: In a main menu defines the CTRL + C, CTRL + V, ctrl+t, Delete, such as shortcut key operation of the main interface, The DataGridView of MDI child forms requires both the ability to manipulate one row of the table with these shortcuts, and the ability to manipulate cell contents in the editing state.

Copy, paste, cut, delete DataGridView a line of content on the internet has a lot of information, nor is this the focus of this article, do not repeat here; DataGridView with Ctrl + C, ctrl+vctrl+c, Ctrl + V, Ctrl+t, Delete and other shortcut key operations. It is arguably possible to combine the two to achieve functionality, but they are not compatible when using shortcut keys.

Following the CTRL + C copy operation to illustrate that the other operations are described under CTRL + C copy operations, the other methods are similar:
The shortcut keys for the main menu will block the DataGridView with a quick operation. In the software is the copy function that invokes the subform in the copy function of the main form:
<summary>
Copy
</summary>
private void Tsbtncopy_click (object sender, EventArgs e)
{
if (Instance.activemdichild is Iedit)
{
Iedit form = Instance.activemdichild as Iedit;
Form. Copy ();
}
}

This makes the entire row of replication completely OK, but the contents of the cell are not replicated, and whenever you press CTRL + C, you go to the Tsbtncopy_click method, and then to the copy function of the subform, because the result of the main Menu shortcut screen , there are two ways to implement the contents of a replicated cell:
1. Increase the copy of cell selection in the Copy method
2. Find ways to call the DataGridView copy method when the cell editing state

Idea 1:
To achieve the editing of the contents of DataGridView cells, you need to get the editing status of the contents of the current cell; "Cursor position", "selection" these are not known; decisively give up the idea.
Idea 2:
Press the shortcut key, called the subform copy function, you can determine in the function body whether there is a cell in the editing state, if there is a manual call DataGridView copy method; But the DataGridView method does not have this realization; All editable boxes, such as TextBox, ComboBox, and ListBox, have their own copy operations, which should be the function of the parent class or interface inherited by these controls and are hidden.

Since it is not possible to invoke DataGridView replication directly, you can trigger the DataGridView copy method or simulate CTRL + C key message in the subform; Reference http://blog.csdn.net/zhbog/article/ DETAILS/6751346 Bowen triggers the CTRL + C key message in the replication method, and DataGridView still cannot copy the cell contents.

There is nothing to think of, and there are other things to put in abeyance ... After a week of occasional thinking about this problem, why not fundamentally solve the problem of key messages. Now that the menu shortcuts block DataGridView Shortcuts, it's a quick operation to screen the menu keys when DataGridView is in edit state.
So when the DataGridView enters and leaves the edit state, modify the Response menu shortcut key:

<summary>
Edit Status, screen menu shortcut keys
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Datagridview1_cellbeginedit (object sender, DataGridViewCellCancelEventArgs e)
{
MainForm.Instance.EnableCopy (FALSE);
}


<summary>
To leave the edit State, restore the menu shortcut keys
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Datagridview1_cellendedit (object sender, DataGridViewCellEventArgs e)
{
MainForm.Instance.EnableCopy (TRUE);
}


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.