Asp tutorial. net datagridview Copy to clipboard method
Copy mode settings
Maid copymode = maid copymode. enablewithoutheadertext // sets the replicable mode
The datagridview. clipboardcopymode attribute gets or sets a value, which indicates whether the user can copy the text value of the cell to the clipboard, and whether the row title and column title text are included.
Namespace: system. windows. forms
Assembly: system. windows. forms (in system. windows. forms. dll)
Selected copy
Clipboard. setdataobject (datagridview1.getclipboardcontent () // place the data selected by the control to the system clipboard.
Datagridview paste
Code
If (MAID. value = null)
{
Return;
}
Int insertrowindex = maid. rowindex;
String pastetext = clipboard. gettext (); // obtain data from the system clipboard
If (string. isnullorempty (pastetext ))
{
Return;
}
String [] lines = pastetext. split ('r'); // group by row
Bool isheader = true;
Foreach (string line in lines)
{
If (isheader)
{
Isheader = false; // filter operations when the title is included in the replicable mode
}
Else
{
String [] vals = line. split ('t'); // grouping by tab space
If (vals. length-1! = Maid)
{
Throw new applicationexception ("column number error ");
}
Maid row = maid [insertrowindex];
Row. headercell. value = vals [0];
For (int I = 0; I <row. cells. count-1; I ++)
{
Row. cells [I]. value = vals [(I + 1)];
}
Insertrowindex + = 1;
}
}
IX. Automatic ID of the datagridview
Code
Private void maid (object sender, maid e)
{
// The automatic number is irrelevant to the database tutorial.
Rectangle = new rectangle (e. rowbounds. location. x, e. rowbounds. location. y
, Maid-4, e. rowbounds. height );
Textrenderer. drawtext (e. graphics, (e. rowindex + 1). tostring (), datagridview1.
Rowheadersdefaultcellstyle. font, rectangle,
Datagridview1.rowheadersdefaultcellstyle. forecolor, textformatflags. verticalcenter | textformatflags. right );
}
Show row number