如何在winform DataGridView控件的DataGridViewButtonColumn按钮列中禁用按钮

摘要:
原文:http://msdn.microsoft.com/en-us/library/ms171619(v=vs.85)。ASPX公共类DataGridViewDisableButtonColumn:DataGridViewButtonColumn{publicDataGridViewDisableButtonColumn(){this.CellTemplate=newDataGr

原文:http://msdn.microsoft.com/en-us/library/ms171619(v=vs.85).ASPX

public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn
    {
        public DataGridViewDisableButtonColumn()
        {
            this.CellTemplate = new DataGridViewDisableButtonCell();
        }
    }
/// <summary>
/// DataGridView的DataGridViewButtonCell没有可以禁用按钮的功能,这里自定义一个控件实现这个功能
/// </summary>
public class DataGridViewDisableButtonCell : DataGridViewButtonCell
{
    private bool enabledValue;

    /// <summary>
    /// 是否使能
    /// </summary>
    public bool Enabled
    {
        get
        {
            return enabledValue;
        }
        set
        {
            enabledValue = value;
        }
    }

    // Override the Clone method so that the Enabled property is copied.
    public override object Clone()
    {
        DataGridViewDisableButtonCell cell =
            (DataGridViewDisableButtonCell)base.Clone();
        cell.Enabled = this.Enabled;
        return cell;
    }

    // By default, enable the button cell.
    public DataGridViewDisableButtonCell()
    {
        this.enabledValue = true;
    }

    protected override void Paint(Graphics graphics,
        Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
        DataGridViewElementStates elementState, object value,
        object formattedValue, string errorText,
        DataGridViewCellStyle cellStyle,
        DataGridViewAdvancedBorderStyle advancedBorderStyle,
        DataGridViewPaintParts paintParts)
    {
        // The button cell is disabled, so paint the border,  
        // background, and disabled button for the cell.
        if (!this.enabledValue)
        {
            // Draw the cell background, if specified.
            if ((paintParts & DataGridViewPaintParts.Background) ==
                DataGridViewPaintParts.Background)
            {
                SolidBrush cellBackground =
                    new SolidBrush(cellStyle.BackColor);
                graphics.FillRectangle(cellBackground, cellBounds);
                cellBackground.Dispose();
            }

            // Draw the cell borders, if specified.
            if ((paintParts & DataGridViewPaintParts.Border) ==
                DataGridViewPaintParts.Border)
            {
                PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
                    advancedBorderStyle);
            }

            // Calculate the area in which to draw the button.
            Rectangle buttonArea = cellBounds;
            Rectangle buttonAdjustment =
                this.BorderWidths(advancedBorderStyle);
            buttonArea.X += buttonAdjustment.X;
            buttonArea.Y += buttonAdjustment.Y;
            buttonArea.Height -= buttonAdjustment.Height;
            buttonArea.Width -= buttonAdjustment.Width;

            // Draw the disabled button.                
            ButtonRenderer.DrawButton(graphics, buttonArea,
                PushButtonState.Disabled);

            // Draw the disabled button text. 
            if (this.FormattedValue is String)
            {
                TextRenderer.DrawText(graphics,
                    (string)this.FormattedValue,
                    this.DataGridView.Font,
                    buttonArea, SystemColors.GrayText);
            }
        }
        else
        {
            // The button cell is enabled, so let the base class 
            // handle the painting.
            base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                elementState, value, formattedValue, errorText,
                cellStyle, advancedBorderStyle, paintParts);
        }
    }
}

免责声明:文章转载自《如何在winform DataGridView控件的DataGridViewButtonColumn按钮列中禁用按钮》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇敏捷的原则和价值观转:ECharts图表组件入门教程之Theme:ECharts图表的皮肤是什么?如何给图表换主题(皮肤)Theme?下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

layerui如何隐藏按钮?

https://www.layui.com/doc/modules/layer.html#btn 建议把 btn: ['取消'],btnAlign: 'c',yes: function (index) { layer.close(index); //layer.close(layer.index); }这一段配置去除即可,就不会出现按钮了,...

C# datagridview 的属性及事件

1.CellPainting: 在datagridview重绘的时候触发此事件 2。RowPostPaint: 在单元格重绘之后触发此事件 3.DataGridView.GetCellDisplayRectangle 方法 :返回表示单元格的显示区域的矩形。 public Rectangle GetCellDisplayRectangle( int co...

Winform开发框架之对话框样式同化

早在《Winform分页控件重大更新,并实现普通版、DotNetBar、DevExpress三大版本整合更新(附各种例子源码)》随笔中,就集成了三种界面样式的分页控件(普通版、基于DotNetBar样式和基于DevExpress样式三种),后来也就形成了我三种不同的Winform开发框架界面样式。 1)基于传统经典模式的界面,采用了OutLookBar工具...

WPF Button添加图片

0、更改模板 效果: 代码: <Button x:Name="m_HelpButton"IsEnabled="True"Width="23"Height="23"Click="m_HelpButton_Click"> <Button.Template> <...

MFC入门(二)

1 基于对话框学习控件   1.1 对话框 模态非模态   1.2 按钮 两个 captain修改内容  直接输入内容   1.3 点击触发事件           右侧属性 闪电图标 ;         右键按钮 添加事件处理程序 ;         双击按钮,(只能是点击事件)   1.4 插入窗口  窗口 右键 添加类   1.5 模态窗口创建...

在Winform界面中使用DevExpress的TreeList实现节点过滤查询的两种方式

在我较早的一篇随笔《在DevExpress程序中使用TeeList控件以及节点查询的处理》中,介绍了在树形列表TreeList控件上面,利用SearchControl实现节点的模糊查询过滤操作,效果还是非常不错的,TreeList功能比较强大,界面也相对比微软内置的Winform的TreeView控件美观不少。后来在一次偶然过程中,发现TreeList控件...