Copy the cell area data in the datagridview instance like Excel

Source: Internet
Author: User

Copy the cell area data in the datagridview instance like Excel:

Implementation idea: add the selected content to the clipboard using the clipboard class, then read the information in the clipboard using this class, and then read the position of the selected cell in the datagridview control, add the selected cell to the new dview.

 

The setdataobject method places data on the system clipboard. The common syntax is as follows:

Clipboard. setdataobject (dgview. getclipboardcontent ())

 

The common syntax of the gettext method to retrieve text data from the cut version is as follows:

Public static string gettext ()

 

The methods of the clipboard class are not described here

The attributes of the datagridview, such as the selectedrows attribute, are used to obtain the set of rows selected by the user.

The Rows attribute obtains a set that includes all rows.

Clear Control

Select multiple rows for multiselect

Fullrowselect selects its subitem and so on.

In addition, Let's briefly describe the datagridview control.

First, you need to distinguish between several components. You can open my computer and choose to view details to observe that it is actually a dview control.

The colums column set is the total name type size. The available space is the entire column set.

Items is equivalent to a set of items that you have selected, such as drive C, drive D.

Listviewitem is one of the items in items, such as drive C.

The subitems subitem set is all options except the subitems subitem

Listviewsubitem is a subitem in subviewsubitem.

The total size of the following types of available space is actually the name of the younger brother name is Big Brother

If the data is operated, this name is a symbolic item. Generally, it is the ID column in the database, and the primary key is put into the tag so that the user is invisible.

Our operators can operate on it again.

 

Select the cell to be copied in the datagridview1 control.

Add the cell content to datagridview2:

Private void button#click (Object sender, eventargs E );

 

Custom method:

Public String copydatagridview (datagridgdgview)

Adds the content of the selected cell to the clipboard, and saves the text information in the clipboard to the string variable;

 

Custom method public void adddatagridview (datagridgdgview, string S, bool blank, bool all)

Is to add the text information dragged in the clipboard to the datagridview2 according to the corresponding format of the string in the text and the corresponding position of the cell.

 

 

 Using system; <br/> using system. collections. generic; <br/> using system. componentmodel; <br/> using system. data; <br/> using system. drawing; <br/> using system. LINQ; <br/> using system. text; <br/> using system. windows. forms; <br/> using system. collections; </P> <p> The namespace is similar to excel in copying the cell area data in the datagridview <br/>{< br/> Public partial class form1: form <br/> {<br/> Public form1 () <br/> {<br/> initializecom Ponent (); <br/>}</P> <p> private void form1_load (Object sender, eventargs E) <br/>{< br/> // Add data to the datagridview <br/> This. datagridview1.columncount = 5; <br/> This. datagridview1.rows. add (New String [] {"1", "2", "3", "4", "5"}); <br/> This. datagridview1.rows. add (New String [] {"six", "Seven", "eight", "Nine", "Ten"}); <br/> This. datagridview1.rows. add (New String [] {"11", "12", "13", "14", "15"}); <br/> This. Datagridview1.rows. add (New String [] {"", "17th", "", "19th", ""}); <br/> This. datagridview1.rows. add (New String [] {"21", "22", "23", "24", "25"}); <br/> This. datagridview1.autoresizecolumns (); <br/> This. datagridview1.clipboardcopymode = datagridviewclipboardcopymode. enablewithoutheadertext; <br/>}</P> <p> private string STR = ""; <br/> private bool bool_blank = false; // whether the cell is empty <br/> P Rivate bool bool_all = false; // whether to replace all </P> <p> private void button#click (Object sender, eventargs E) <br/>{< br/> STR = copydatagridview (datagridview1); <br/> adddatagridview (datagridview2, STR, bool_blank, bool_all ); <br/>}</P> <p> // <summary> <br/> // copy the selected content in the datagridview control using the clipboard. <br/> /// </Summary> <br/> // <Param dgview = "datagridview"> datagridview class </param> <br/> // <return> string </return> <Br/> Public String copydatagridview (datagridview dgview) <br/>{< br/> string tem_str = ""; <br/> If (dgview. getcellcount (maid. selected)> 0) <br/> {<br/> try <br/> {<br/> // Add data to the clipboard <br/> clipboard. setdataobject (dgview. getclipboardcontent (); <br/> // obtain information from the clipboard <br/> tem_str = clipboard. gettext (); <br/>}< br/> catch (system. runtime. interopservices. externalexception) <br/ >{< Br/> return ""; <br/>}< br/> return tem_str; <br/>}</P> <p> // <summary> <br/> // Add the string to the datagridview control in the specified format (if any selected cell, modify the content in the cell ). <br/> /// </Summary> <br/> // <Param dgview = "datagridview"> datagridview class </param> <br/> // <param S = "string"> cell string to be replaced </param> <br/> // <Param blank = "bool"> ID, if the space is replaced with a space </param> <br/> // <Param all = "bool">, replace all </param> <br/> Public void ad Ddatagridview (datagridgview, string S, bool blank, bool all) <br/>{< br/> string tem_str = s; <br/> int tem_n = 0; <br/> int rowcount = 0; // number of rows <br/> int cellcount = 0; // Number of columns <br/> bool tem_bool = true; <br/> string tem_s = ""; <br/> If (S. indexof ("/R/N ")! =-1) // if multiple rows are replaced <br/> while (tem_bool) // obtain the number of rows and columns of cells <br/>{< br/> tem_s = ""; <br/> If (tem_str.indexof ("/R/N ")! =-1) // if the last row is not obtained <br/>{< br/> tem_s = tem_str.substring (0, tem_str.indexof ("/R/N") + 2 ); // obtain data in the current row <br/> // obtain the data that can be identified in the current row <br/> tem_str = tem_str.substring (tem_str.indexof ("/R/N ") + 2, tem_str.length-tem_str.indexof ("/R/N")-2); <br/> tem_n = 1; <br/> while (tem_s.indexof ("/t")> -1) // traverse the space in the current row <br/>{< br/> // remove the read space <br/> tem_s = tem_s.substring (tem_s.indexof ("/t ") + 1, tem_s.length-TEM _ S. indexof ("/t")-1); <br/> tem_n + = 1; // obtain the number of columns <br/>}< br/> If (tem_n> cellcount) // determine whether the current number of columns is the maximum number of columns <br/> cellcount = tem_n; // obtain the maximum number of columns <br/>}< br/> else // if the last row is read <br/>{< br/> tem_n = 1; <br/> while (tem_s.indexof ("/t")>-1) <br/> {<br/> tem_s = tem_s.substring (tem_s.indexof ("/t") + 1, tem_s.length-tem_s.indexof ("/t")-1); <br/> tem_n + = 1; <br/>}< br/> If (tem_n> cellcount) <br/> cellcoun T = tem_n; <br/> tem_bool = false; // The traversal ends. <br/>}< br/> ++ rowcount; // Number of read rows <br/>}< br/> else // If the read data is a single row <br/>{< br/> tem_n = 1; <br/> tem_s = s; <br/> while (tem_s.indexof ("/t")>-1) <br/> {<br/> tem_s = tem_s.substring (tem_s.indexof ("/t") + 1, tem_s.length-tem_s.indexof ("/t")-1 ); <br/> tem_n + = 1; <br/>}< br/> If (tem_n> cellcount) <br/> cellcount = tem_n; <br/> ++ rowcount; // Number of read rows <br/>} <Br/> string [,] strarr = new string [rowcount, cellcount]; // defines an array, used to record the copied cell Information </P> <p> tem_str = s; <br/> tem_n = 0; <br/> // Add the cell information to the array <br/> for (INT I = 0; I <rowcount; I ++) // traverse the row of a cell <br/>{< br/> for (Int J = 0; j <cellcount; j ++) // traverse the cell columns <br/>{< br/> tem_s = ""; <br/> If (tem_str.indexof ("/R/N ")! =-1) // if it is not the last row <br/>{< br/> If (tem_str.indexof ("/t") <=-1) // set the Data Reading location <br/> tem_n = tem_str.indexof ("/R "); // location of the last data <br/> else <br/> tem_n = tem_str.indexof ("/t "); // not the last data location <br/> tem_s = tem_str.substring (0, tem_str.indexof ("/R/N") + 2 ); // read cell data <br/>}< br/> else // if it is the last row <br/>{< br/> If (tem_str.indexof ("/t ") <=-1) // set the Data Reading location <br/> tem_n = tem_str.length; // location of the last data <br/> else <br/> tem_n = tem_str.indexof ("/t "); // not the last data location <br/> tem_s = tem_str; // read the cell data <br/>}< br/> If (tem_s.length> 0) // if the current row contains data <br/>{< br/> If (tem_s.substring (0, 1) = "/t ") // if the first character is null <br/> strarr [I, j] = ""; // Add an empty record to the array <br/> else <br/> {<br/> strarr [I, j] = tem_s.substring (0, tem_n ); // Add data to the array <br/>}< br/> else <br/> strarr [I, j] = ""; // Add an empty record to the array <br/> If (tem_s.length> tem_n) // if the record has not been read <br/> tem_str = tem_s.substring (tem_n + 1, tem_s.length-tem_n-1); // obtain records that have not been read <br/>}< br/> If (S. indexof ("/R/N")>-1) // if not the last row of Data <br/>{< br/> S = S. substring (S. indexof ("/R/N") + 2, S. length-s. indexof ("/R/N")-2); // read the next row of Data <br/> tem_str = s; <br/>}< br/> If (all) // if you want to replace all <br/> dgview. rows. clear (); // clear the datagridview Control <br/> If (dgview. selectedrows. count = 0 & dgview. selectedcells. count = 0) // If the datagridview datagricontains no data <br/>{< br/> dgview. columncount = cellcount; // set the number of columns <br/> string [] stra = new string [cellcount]; // define a one-dimensional array <br/> // Add row data to the datagridview <br/> for (INT I = 0; I <rowcount; I ++) // read rows <br/>{< br/> for (Int J = 0; j <cellcount; j ++) // read the column <br/>{< br/> If (strarr [I, j] = "") // if the current cell is empty <br/>{< br/> If (blank) // if it is replaced by the * sign <br/> stra [J] = "*"; // Add the * number to the empty cell. <br/> else <br/> stra [J] = ""; // display empty cells with spaces <br/>}< br/> else <br/>{< br/> stra [J] = strarr [I, j]; // record information in the current row <br/>}< br/> dgview. rows. add (stra); // Add the information in the row to the datagridview Control </P> <p >}< br/> dgview. autoresizecolumns (); // Add information about all cells to the datagridview control. <br/> dgview. clipboardcopymode = maid copymode. enablewithoutheadertext; // copy the selected cell to the clipboard <br/>}< br/> else // If data exists in the dview <br/>{< br/> int maxrow = 0; // record the number of rows of the smallest cell in the datagridview Control <br/> int maxcell = 0; // record the number of columns of the smallest cell in the datagridview Control <br/> for (INT I = 0; I <dgview. selectedcells. count; I ++) // obtain the maximum number of rows and columns in the selected cell <br/>{< br/> If (dgview. selectedcells [I]. rowindex> maxrow) // if the number of rows in a cell is greater than the number of rows currently specified <br/> maxrow = dgview. selectedcells [I]. rowindex; // record the number of rows in the current cell <br/> If (dgview. selectedcells [I]. columnindex> maxcell) // if the number of columns in a cell is greater than the currently specified number of columns <br/> maxcell = dgview. selectedcells [I]. columnindex; // record the number of columns in the current cell <br/>}< br/> int minrow = maxrow; // record the maximum number of cells in the datagridview Control <br/> int mincell = maxcell; // record the maximum number of cells in the datagridview Control <br/> for (INT I = 0; I <dgview. selectedcells. count; I ++) // obtain the number of rows and columns of the smallest cell in the selected cell <br/>{< br/> If (dgview. selectedcells [I]. rowindex <minrow) // if the number of rows in a cell is smaller than the number of rows currently specified <br/> minrow = dgview. selectedcells [I]. rowindex; // record the number of rows in the current cell <br/> If (dgview. selectedcells [I]. columnindex <mincell) // if the number of columns in a cell is smaller than the currently specified number of columns <br/> mincell = dgview. selectedcells [I]. columnindex; // record the number of columns of the current cell <br/>}< br/> // Add all cells in the minimum and maximum cells of the selected cell to the datagridview Control <br/> (int I = 0; I <maxrow-(minrow-1); I ++) // traverses the number of rows <br/>{< br/> if (I> = rowcount) // if the number of rows to be added exceeds <br/> break; // exit the loop <br/> for (Int J = 0; j <maxcell-(mincell-1 ); j ++) // traverse the number of columns <br/>{< br/> If (j> = cellcount) // if the number of columns to be added exceeds the limit <br/> break; // exit the loop <br/> If (strarr [I, j] = "") // If the added cell is empty <br/>{< br/> If (blank) // if the space is replaced by the * sign <br/> dgview. rows [I + minrow]. cells [J + mincell]. value = "*"; // replace an empty cell with the number * <br/>}< br/> else <br/> dgview. rows [I + minrow]. cells [J + mincell]. value = strarr [I, j]; // set the value of the current cell <br/>}</P> <p> private void tool_blk_click (Object sender, eventargs e) <br/>{< br/> If (toolstripmenuitem) sender ). checked) // if this item is selected <br/>{< br/> (toolstripmenuitem) sender ). checked = false; // uncheck this option <br/> bool_blank = false; // empty cells do not need to be replaced by a number * <br/>}< br/> else <br/>{< br/> (toolstripmenuitem) sender ). checked = true; // set this item to be selected <br/> bool_blank = true; // replace empty cells with the asterisk (*) <br/>}</P> <p> private void tool_all_click (Object sender, eventargs E) <br/>{< br/> If (toolstripmenuitem) sender ). checked) // if this item is selected <br/>{< br/> (toolstripmenuitem) sender ). checked = false; // cancel the selected status. <br/> bool_all = false; // replace all <br/>}< br/> else <br/>{< br/> (toolstripmenuitem) sender ). checked = true; // set this item to be selected <br/> bool_all = true; // Replace the selected part <br/>}< br/>}

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.