Usage summary of the datagridview

Source: Internet
Author: User

 

1 ). Right-click the row and select the row. The operation menu is displayed.

1 ). Add a shortcut menu contextmenustrip1;
2 ). Add processing for the cellmousedown event of datagridview1 Program : Private Void Maid ( Object Sender, datagridviewcellmouseeventargs E)
{
If (E. Button = mousebuttons. Right)
{
// Filter row titles and blank column headers that may be right-clicked.
If (E. rowindex> = 0 & Amp; E. columnindex & gt; = 0 )
{

// If the row is selected, no setting is performed.
If (Maid [E. rowindex]. Selected = False )
{
Datagridview1.clearselection ();
Datagridview1.rows [E. rowindex]. Selected = True ;
}
// Set active cells when only one row is selected
If (Maid. Count = 1 )
{
Datagridview1.currentcell = maid [E. rowindex]. cells [E. columnindex];
}
// Operation menu
Contextmenustrip1.show (mouseposition. X, mouseposition. y );
}
}

}

:

2). Copy the content of the selected cell to the clipboard.

Clipboard. setdataobject (maid ());

3). Only display custom Columns

Datagridview1.autogeneratecolumns =False;//Must be inCodeSet in

4). Display image

In general, we save the image path in the database, but to display the image in datagridview1, you can do the following:

① Add a column of the datagridviewtextboxcolumn type, name = path, datapropertyname = pic, visible = false;
②. Add a column of the datagridviewimagecolumn type, name = PIC;
③. The databindingcomplete event handler for the datagridview1 control is as follows:

private void datagridview1_databindingcomplete ( Object sender, datagridviewbindingcompleteeventargs E)
{< br> image image1 = null ;
image image2 = null ;
string Path = string . empty;

For(IntI =0; I <maid. Count; I ++)
{
Path =@"F :\"+ Datagridview1.rows [I]. cells ["Path"]. Value;

If (File. exists (PATH ))
{
Image1 = image. fromfile (PATH );
Image2 = New Bitmap (image1, 120 , 120 ); // Reset size

Datagridview1.rows [I]. cells [ " PIC " ]. Value = image2;
// (Maid) maid [I]. cells ["pic"]). value = image2;
}
}
}

5). When the grid is not filled with the control, draw a line to fill the blank area.

///   <Summary>
/// Draw a grid to fill the blank area
///   </Summary>
///   <Param name = "sender"> </param>
///   <Param name = "E"> </param>
Public Void Cellpainting ( Object Sender, datagridviewcellpaintingeventargs E)
{
Datagridview mydatagri= (datagridview) sender;

If (Mydatagridview. Rows. Count> 0 )
{
Int I = mydatagridview. columnheadersheight; // Title line height
Int J = mydatagridview. Rows. getrowsheight (maid. Visible ); // Total height of all visible lines
Int K = mydatagridview. height; // Control height
Int L = mydatagridview. Rows. getlastrow (maid. Visible ); // Last Row Index
Int Count = mydatagridview. Columns. count; // Total number of Columns
Int Width = 0 ;

// Draw a line when the grid is not filled with controls
If (I + j <K)
{
Using (Brush gridbrush = New Solidbrush (mydatagridview. gridcolor ))
{
Using (Pen gridlinepen = New Pen (gridbrush ))
{
// Processing title Columns
If (Mydatagridview. rowheadersvisible)
{
Width = mydatagridview. rowheaderswidth;
E. Graphics. drawline (gridlinepen, width, I + J, width, k );
}
Else
{
Width = 1 ;
}

// Processed Columns
For ( Int Index = 0 ; Index <count; index ++)
{
If (Mydatagridview. Columns [Index]. Visible)
{
Width + = mydatagridview. Columns [Index]. width;

E. Graphics. drawline (gridlinepen, width, I + J, width, k );
}
}
}
}
}
}
}

6). Custom column width

Manually add columns and set the width one by one on the edit columns page. Note that the autosizecolumnsmode value must be datagridviewautosizecolumnsmode. None. Otherwise, the custom width cannot take effect!

7). Format the cell content

Private Void Maid ( Object Sender, datagridviewcellformattingeventargs E)
{
Datagridview mydatagri= (datagridview) sender;
If (Mydatagridview. Columns [ " ID " ]. Index = E. columnindex)
{
If (E. value! = Null &&! String . Isnullorempty (E. value. tostring ()))
E. value = " BH " + String . Format ( " {0: D8} " , Int . Parse (E. value. tostring ()));
}
}

8). Create a beautiful datagridview

// Style
Datagridview1.autosizecolumnsmode = maid. None; // The column width is not automatically adjusted. manually add columns.
Datagridview1.rowheaderswidth = 12 ;// The row title is fixed to 12 characters in width.
Datagridview1.rowheaderswidthsizemode = datagridviewrowheaderswidthsizemode. disableresizing; // You cannot adjust the column header width with the mouse.
Datagridview1.alternatingrowsdefaultcellstyle. backcolor = color. lemonchiffon; // Background Color of odd rows
Datagridview1.backgroundcolor = color. White; // Control background color
Datagridview1.columnheadersdefaultcellstyle. Alignment = maid. middlecenter; // Center Column Title display
Datagridview1.defaultcellstyle. Alignment = maid. middlecenter; // Center cell content display

// Action
Datagridview1.autogeneratecolumns = False ; // Columns not automatically created
Datagridview1.allowusertoaddrows = False ; // Do not enable add
Datagridview1.readonly = True ; // Edit disabled
Datagridview1.allowusertodeleterows = False ; // Disable Deletion
Datagridview1.selectionmode = datagridviewselectionmode. fullrowselect; // Click the cell to select the entire row
Datagridview1.multiselect = False ; // You cannot select multiple

9 ). Check whether a scroll bar exists.

// vertical scroll bar
If (datagridview1.rows. getrowsheight (maid. none)> datagridview1.height)
MessageBox. show ( " Yes " );
else
MessageBox. show ( " none " );

//Horizontal scroll bar
If(Maid. getcolumnswidth (maid. None)> maid)
MessageBox. Show ("Yes");
Else
MessageBox. Show ("None");

10). Why is the column title always not centered?

The default alignment of Column Titles has been set to center:

Datagridview1.columnheadersdefaultcellstyle. Alignment = maid. middlecenter;//Center Column Title display

However, the actual effect is always a little left, because the column can be sorted, and the sorting symbol occupies space on the column title. Column-by-column settings can be removed:

Maid [I]. sortmode = maid. notsortable;

 

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.