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;