Solution of copying selected cell content by DataGridView in WinForm

Time:2020-10-22

Data GridView mouse selected cell content replication scheme in WinForm

1. CTR + C shortcut key copy

Premise: the clipboardcopymode property of the control is not disabled;

 

 

 

2. Mouse box selection, user-defined code to copy

Datagridview1. Selectedcells can get all the selected cells in a single list. However, when traversing the cells, it is found that the order of the cells may not be consistent with the display order of the interface.

The selectionmode property in DataGridView is order dependent

 

 

The grid is determined by outputting row and column index of each cell. The code is as follows:

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
         {
             var currcell = dataGridView1.CurrentCellAddress;
             Console.WriteLine ($"current cell {currcell. Y} -- {currcell. X}");
             var cells = dataGridView1.SelectedCells;
             Console.WriteLine ($"number of cells selected{ cells.Count }");
             for (int i = 0; i < cells.Count; i++)
             {
                 Console.WriteLine ($"grid {I + 1}: row: {cells [i]. Rowindex} column: {cells [i]. Columnindex}");
             }
         }

 

2.1selectionMode=FullRowSelect

case1:

Box cell collection element order

Current cell 4 — 2
Number of selected cells 33
The first grid: row: 4 columns: 0
Grid 2: row: 4 columns: 1
The third grid: row: 4 columns: 2
Grid 4: rows: 4 columns: 3
Grid 5: row: 4 column: 4
The 6th grid: row: 4 columns: 5
Grid 7: rows: 4 columns: 6
8th grid: row: 4 columns: 7
The 9th grid: row: 4 columns: 8
10th grid: row: 4 columns: 9
11th grid: row: 4 columns: 10
The 12th grid: row: 3 columns: 0
Grid 13: rows: 3 columns: 1
Grid 14: rows: 3 columns: 2
15th grid: row: 3 columns: 3
16th grid: row: 3 columns: 4
17th grid: row: 3 columns: 5
18th grid: row: 3 columns: 6
19th grid: row: 3 columns: 7
20th grid: row: 3 columns: 8
Grid 21: rows: 3 columns: 9
Grid 22: row: 3 columns: 10
The 23rd grid: row: 2 columns: 0
The 24th grid: row: 2 columns: 1
The 25th grid: row: 2 columns: 2
Grid 26: rows: 2 columns: 3
Grid 27: rows: 2 columns: 4
28th grid: row: 2 columns: 5
Grid 29: rows: 2 columns: 6
Grid 30: rows: 2 columns: 7
Grid 31: rows: 2 columns: 8
The 32nd grid: row: 2 columns: 9
33rd grid: row: 2 columns: 10

case2:

Box cell collection element order

Current cells 9 — 5
Number of selected cells 33
Grid 1: row: 9 column: 0
Grid 2: row: 9 column: 1
Grid 3: row: 9 column: 2
Grid 4: row: 9 column: 3
Grid 5: row: 9 column: 4
Grid 6: row: 9 column: 5
Grid 7: rows: 9 columns: 6
Grid 8: row: 9 column: 7
The 9th grid: row: 9 column: 8
10th grid: row: 9 column: 9
11th grid: row: 9 columns: 10
The 12th grid: row: 10 column: 0
Grid 13: row: 10 column: 1
Grid 14: row: 10 column: 2
The 15th grid: row: 10 column: 3
16th grid: row: 10 columns: 4
Grid 17: rows: 10 columns: 5
Grid 18: rows: 10 columns: 6
19th grid: row: 10 columns: 7
20th grid: row: 10 columns: 8
Grid 21: rows: 10 columns: 9
Grid 22: rows: 10 columns: 10
Grid 23: rows: 11 columns: 0
Grid 24: rows: 11 columns: 1
Grid 25: row: 11 column: 2
Grid 26: rows: 11 columns: 3
Grid 27: rows: 11 columns: 4
Grid 28: rows: 11 columns: 5
Grid 29: rows: 11 columns: 6
Grid 30: rows: 11 columns: 7
Grid 31: rows: 11 columns: 8
The 32nd grid: rows: 11 columns: 9
Grid 33: rows: 11 columns: 10

2.2selectionMode=CellSelect

case1:

 

Box cell collection element order

Current cell 3 — 4
Number of selected cells 9
Grid 1: row: 3 columns: 4
Grid 2: row: column 2: 4
Grid 3: row: 1 column: 4
Grid 4: rows: 3 columns: 3
The fifth grid: row: 3 columns: 2
Grid 6: row: 2 columns: 3
Grid 7: row: 1 column: 3
8th grid: row: 2 columns: 2
The 9th grid: row: 1 column: 2

case2:

 

Box cell collection element order

Current cell 5 — 2
Number of selected cells 6
The first grid: row: 5 columns: 2
Grid 2: row: 4 columns: 2
The third grid: row: 3 columns: 2
Grid 4: row: 2 column: 2
Grid 5: row: 1 column: 2
Cell 6: row: 0 column: 2

case3:

 

Box cell collection element order

Current cell 6 — 4
Number of selected cells 10
Grid 1: row: 6 columns: 5
Grid 2: row: 6 columns: 4
Grid 3: row: 7 column: 5
Grid 4: row: 7 column: 4
Grid 5: row: 10 column: 4
Grid 6: row: 9 column: 4
Grid 7: rows: 8 columns: 4
Grid 8: row: 8 column: 5
The 9th grid: row: 9 column: 5
The 10th grid: row: 10 column: 5

case4:

 

Box cell collection element order

Current cell 7 — 5
Number of selected cells 6
The first grid: row: 7 columns: 5
Grid 2: row: 8 column: 5
Grid 3: row: 9 column: 5
Grid 4: row: 10 column: 5
Grid 5: row: 11 column: 5
Grid 6: row: 12 column: 5

case5:CTR + mouse click

 

Box cell collection element order

Current cell 0 — 4
Number of selected cells 7
Grid 1: row: 0 column: 4
Grid 2: rows: 15 columns: 5
Grid 3: row: 12 column: 2
Grid 4: rows: 2 columns: 8
Grid 5: row: 8 column: 5
Grid 6: row: 5 column: 3
Grid 7: rows: 3 columns: 2

case7: Shift + mouse click

 

Box cell collection element order

Current cell 8 — 2
Number of selected cells 8
Row 8: row 1
Grid 2: row: 7 column: 2
The third grid: row: 6 columns: 2
Grid 4: row: 5 column: 2
The fifth grid: row: 4 columns: 2
The 6th grid: row: 3 columns: 2
Grid 7: rows: 2 columns: 2
8th grid: row: 1 column: 2

case7: Shift + mouse click

Box cell collection element order

Current cell 7 — 4
Number of selected cells 12
Grid 1: row: 7 columns: 4
Grid 2: row: 6 columns: 4
Grid 3: row: 5 column: 4
Grid 4: row: 7 column: 3
The 5th grid: row: 6 columns: 3
Grid 6: row: 5 column: 3
Grid 7: row: 7 column: 2
8th grid: row: 6 columns: 2
The 9th grid: row: 5 columns: 2
10th grid: row: 4 columns: 4
11th grid: row: 4 columns: 3
Column 4: 2

 summary

There is no obvious rule to add selected cells in the set. Some people in the blog suggest that the elements in the selected area can be determined by selecting the first and last element of the set of cells. It can be seen that through the above experiment, this practice is obviously wrong.

Solution:

1. You can traverse the cell set, rearrange the element positions according to the order of first column and then column, and then traverse the cell to obtain the cell value and splice it to the clipboard. However, the efficiency of this method should be lower when there is a large amount of data.

 

Recommended Today

What is “hybrid cloud”?

In this paper, we define the concept of “hybrid cloud”, explain four different cloud deployment models of hybrid cloud, and deeply analyze the industrial trend of hybrid cloud through a series of data and charts. 01 introduction Hybrid cloud is a computing environment that integrates multiple platforms and data centers. Generally speaking, hybrid cloud is […]