winform的DataGridView中用C#實現按鈕列置灰

來源:互聯網
上載者:User

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;
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.