Use C # To implement button columns in the winform datagridview

Source: Internet
Author: User

The datagridview control includes the datagridviewbuttoncell class, which is used to display cells with user interfaces (UIS) similar to buttons. However, datagridviewbuttoncell does not disable the appearance of buttons displayed by cells.

The followingCodeThe following example shows how to customize the datagridviewbuttoncell class to display buttons that can be displayed as disabled. This example defines a new cell type, which is derived from datagridviewbuttoncell. This cell type provides a new enabled attribute, which can be set to false to draw a disabled button in the cell. This example also defines a new column type: datagridviewdisablebuttoncolumn, which displays the datagridviewdisablebuttoncell object. To demonstrate the new cell type and column type, the current value of each datagridviewcheckboxcell in the parent dview determines whether the enabled attribute of the datagridviewdisablebuttoncell in the same row is true or false.

First, add the following two inheritance classes to the Code:

Public class datagridviewdisablebuttoncolumn: datagridviewbuttoncolumn {public datagridviewdisablebuttoncolumn () {This. celltemplate = new inline () ;}} public class maid: Maid {private bool enabledvalue; Public bool enabled {get {return enabledvalue;} set {enabledvalue = value ;}} public override object clone () {datagridviewdisablebuttoncell Cell = (Maid) base. Clone (); Cell . Enabled = This. enabled; Return Cell ;} Public datagridviewdisablebuttoncell () {This. protected = true;} protected override void paint (Graphics graphics, rectangle finished, rectangle cellbounds, int rowindex, datagridviewelementstates elementstate, object value, object success, string errortext, datagridviewcellstyle cellstyle, datagridviewadvancedborderstyle cedborderstyle, datagridviewpaintparts paintparts) {if (! This. enabledvalue) {If (paintparts & datagridviewpaintparts. background) = maid. background) {solidbrush cellbackground = new solidbrush (cellstyle. backcolor); graphics. fillrectangle (cellbackground, cellbounds); cellbackground. dispose ();} If (paintparts & datagridviewpaintparts. border) = maid. border) {paintborder (graphics, clipbounds, cellbounds, cellstyle, advancedborderstyle);} rectangle buttonarea = cellbounds; rectangle buttonadjustment = This. borderwidths (advancedborderstyle); buttonarea. X + = buttonadjustment. x; buttonarea. Y + = buttonadjustment. y; buttonarea. height-= buttonadjustment. height; buttonarea. width-= buttonadjustment. width; buttonrenderer. drawbutton (graphics, buttonarea, system. windows. forms. visualstyles. pushbuttonstate. disabled); If (this. formattedvalue is string) {textrenderer. drawtext (graphics, (string) This. formattedvalue, this. datagridview. font, buttonarea, systemcolors. graytext) ;}} else {base. paint (graphics, clipbounds, cellbounds, rowindex, elementstate, value, formattedvalue, errortext, cellstyle, advancedborderstyle, paintparts );}}}

TheButtonAdd the following controls to the Code:
For example, add the edit button:

 
Datagridviewdisablebuttoncolumn btn_proedit = new datagridviewdisablebuttoncolumn (); btn_proedit.headertext = L. alanuagebinding ("gv_action"); btn_proedit.text = L. alanuagebinding ("gv_edit"); btn_proedit.name = "btn_proedit"; btn_proedit.width = 50; btn_proedit.usecolumntextforbuttonvalue = true; this. datagridview1.columns. add (btn_proedit );

Disable:

 
Datagridviewdisablebuttoncell buttoncell = (datagridviewdisablebuttoncell) dgv_promotiom.rows [I]. cells ["btn_proedit"]; buttoncell. Enabled = false;

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.