// 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); }
// If the user clicks on an enabled button cell, this event handler // reports that the button is enabled. void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (dataGridView1.Columns[e.ColumnIndex].Name == "Buttons") { DataGridViewDisableButtonCell buttonCell = (DataGridViewDisableButtonCell)dataGridView1. Rows[e.RowIndex].Cells["Buttons"]; if (buttonCell.Enabled) { MessageBox.Show(dataGridView1.Rows[e.RowIndex]. Cells[e.ColumnIndex].Value.ToString() + " is enabled"); } } }
// If a check box cell is clicked, this event handler disables // or enables the button in the same row as the clicked cell. public void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (dataGridView1.Columns[e.ColumnIndex].Name == "CheckBoxes") { DataGridViewDisableButtonCell buttonCell = (DataGridViewDisableButtonCell)dataGridView1. Rows[e.RowIndex].Cells["Buttons"]; DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1. Rows[e.RowIndex].Cells["CheckBoxes"]; buttonCell.Enabled = !(Boolean)checkCell.Value; dataGridView1.Invalidate(); } }