首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#怎么禁用datagridview控件中的button控件

2012-10-11 
C#如何禁用datagridview控件中的button控件DataGridView 控件包括 DataGridViewButtonCell 类,该类用于显

C#如何禁用datagridview控件中的button控件
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;

热点排行