DataGridView特殊按鈕(DisableButtonColumn)

實現的效果如下圖,點擊一次按鈕之後,按鈕不可再點擊:


主要代碼如下:

        private void AddButtonColumn()
        {
            DataGridViewDisableButtonColumn column1 = new DataGridViewDisableButtonColumn();
            column1.Name = "allowinpacuButton";
            column1.HeaderText = "";
            column1.DefaultCellStyle.NullValue = "批准";
            dataGridView_pacuRecord.Columns.Add(column1);
        }

    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;
            }
        }

        // 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);
            }
        }
    }

private void dataGridView_pacuRecord_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex>-1)
            {
                if (this.dataGridView_pacuRecord.Columns[e.ColumnIndex].Name == "allowinpacuButton")
                {
                    if (this.dataGridView_pacuRecord.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null
|| this.dataGridView_pacuRecord.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "批准")
                    {
                        DataGridViewDisableButtonCell  buttonCell =(DataGridViewDisableButtonCell )dataGridView_pacuRecord.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        buttonCell.Enabled = false;

                        string patient_id = this.dataGridView_pacuRecord.CurrentRow.Cells["patient_id"].Value.ToString();
                        decimal visit_id = decimal.Parse(this.dataGridView_pacuRecord.CurrentRow.Cells["visit_id"].Value.ToString());
                        decimal oper_id =decimal.Parse(this.dataGridView_pacuRecord.CurrentRow.Cells["oper_id"].Value.ToString());
                        string operating_room_no = this.dataGridView_pacuRecord.CurrentRow.Cells["operating_room_no"].Value.ToString();

                        if (new AnesMasterBC().AllowInPacu(patient_id, visit_id, oper_id, operating_room_no))
                        {
                            this.dataGridView_pacuRecord.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "已批准";                            
                        }
                        
                    }
                }
            }
      
        }




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章