转载:winform的DataGridView中用C#实现按钮列置灰

摘要:
下面的代码示例演示如何自定义DataGridViewButtonCell类来显示可以显示为禁用的按钮。为了演示此新单元格类型和列类型,父DataGridView中的每个DataGridViewCheckBoxCell的当前值确定同一行中DataGridViewDisableButtonCell的Enabled属性是true还是false。

DataGridView 控件包括 DataGridViewButtonCell 类,该类用于显示具有类似按钮的用户界面 (UI) 的单元格。但 DataGridViewButtonCell 不提供禁用由单元格显示的按钮外观的方式。
下面的代码示例演示如何自定义 DataGridViewButtonCell 类来显示可以显示为禁用的按钮。本示例定义一个新的单元格类型 DataGridViewDisableButtonCell,它由 DataGridViewButtonCell 派生。此单元格类型提供一个新的 Enabled 属性,可以将该属性设置为 false 来在单元格中绘制禁用的按钮。本示例还定义一个新的列类型 DataGridViewDisableButtonColumn,它显示 DataGridViewDisableButtonCell 对象。为了演示此新单元格类型和列类型,父 DataGridView 中的每个 DataGridViewCheckBoxCell 的当前值确定同一行中 DataGridViewDisableButtonCell 的 Enabled 属性是 true 还是 false。
首先要在代码中加入下面两个继承类:

public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn
    {
        public DataGridViewDisableButtonColumn()
        {
            this.CellTemplate = new DataGridViewDisableButtonCell();
        }
    }

    public class DataGridViewDisableButtonCell : DataGridViewButtonCell
    {
        private bool enabledValue;
        public bool Enabled
        {
            get
            {
                return enabledValue;
            }
            set
            {
                enabledValue = value;
            }
        }

        public override object Clone()
        {
            DataGridViewDisableButtonCell cell =
                (DataGridViewDisableButtonCell)base.Clone();
            cell.Enabled = this.Enabled;
            return 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)
        {
            if (!this.enabledValue)
            {
                if ((paintParts & DataGridViewPaintParts.Background) ==
                    DataGridViewPaintParts.Background)
                {
                    SolidBrush cellBackground =
                        new SolidBrush(cellStyle.BackColor);
                    graphics.FillRectangle(cellBackground, cellBounds);
                    cellBackground.Dispose();
                }

                if ((paintParts & DataGridViewPaintParts.Border) ==
                    DataGridViewPaintParts.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);
            }
        }
    }

datagridview的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);

禁用:

DataGridViewDisableButtonCell buttonCell = (DataGridViewDisableButtonCell)dgv_Promotiom.Rows[i].Cells["btn_ProEdit"];

                        buttonCell.Enabled = false;

免责声明:文章转载自《转载:winform的DataGridView中用C#实现按钮列置灰》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇手动升级11.2.0.3到12.2.0.1【MongoDB】关于Mongodb的学习与总结下篇

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

相关文章

WinForm中使用DXperience控件中XtraForm,如何实现换肤

在DevExpress.XtraEditors.XtraForm中,窗体的样式和皮肤有UseDefaultLookAndFeel属性。设置为false,就可以直接再属性里面修改样式和皮肤了。如果设置为true,可以再里面放控件defaultLookAndFeel,设置defaultLookAndFeel的样式和皮肤,XtraForm窗体的样式和皮肤就可以随...

winform中如何在TextBox中只能输入数字(可以带小数点)

可以采用像web表单验证的方式,利用textbox的TextChanged事件,每当textbox内容变化时,调用正则表达式的方法验证,用一个label在text后面提示输入错误,具体代码如下: private void textBox1_TextChanged(objectsender, EventArgs e) {...

无服务端纯前台导出数据到Excel-JSExcelXML.js 使用指南

JSExcelXML 使用指南 先来个效果预览  a.前端显示   b.导出excel效果 表头部分   表尾部分   1.功能描述  JsExcelXml 采用js生成excel中可显示的xml格式文本,将输出文本保存为.xls格式即可,可实现所见即所得,该版本和上个版本有较多的改动,现在的版本更加灵活,该版本分别在IE8,9,10,chrome,Fir...

【转】C# WinForm中的Label如何换行

        第一种是把Label的AutoSize属性设为False,手动修改Label的大小.这样的好处是会因内容的长度而自动换行,但是当内容的长度超过所设定的大小时,多出的内容就会无法显示.因此,这种方法适合于基本确定内容长度的时候使用.         第二种是把Label的Dock设为FILL,同时将AutoSize属性设为False,这种方法...

easyExcel自动合并单元格

来源EasyExcel(根据条件动态合并单元格的重复数据) package com.ustcinfo.fn.comp.complain.util; import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.Head; import com.alibaba.e...

C# GridView 操作汇总

GridView 操作汇总 1、自定义列 Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance Host Controls in Windows Forms DataGridVie...