1

Getting error:

Cannot convert type 'bool' to 'System.Windows.Forms.DataGridViewButtonColumn

for line:

(DataGridViewButtonColumn)row.Cells["Recall"].ReadOnly = true;

Any ideas, please

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
SJD
  • 63
  • 5
  • a) don't mix up column and cell b) what would a 'read-only button' even mean?? 'Disabled'? – TaW Jul 18 '16 at 07:17
  • Yes datagridview button for that row will be disabled. – SJD Jul 18 '16 at 07:18
  • Your code is attempting to cast the ReadOnly property of Cells["Recall"] to a DataGridViewButtonColumn then set the column to true. Fix the scope of your cast. – HasaniH Jul 18 '16 at 12:13

2 Answers2

1

There are hints on how to disable a DataGridViewButtonCell or a DataGridViewButtonColumn here and here.

However I'm not sure how much I like them: A lot of work for little gain, afaics..

First the DataGridViewButtonCell is not a real Button anyway. It is rendered as a Button, but that is really just the visuals.

This is different from any other cell type: In a TextCell there is a real TextBox overlayed and for ComboBoxCell or CheckBoxCells likewise real ComboBox and real CheckBox controls are being shown, which can be grabbed and manipulated in the EditingControlShowing event. You can even add event handlers to those controls..

Not so for the ButtonCell. Here you need to code the CellClick or CellContentClick events and query e.g. the e.ColumIndex value to determine the column and usually also the e.RowIndex for the cell that was clicked.

So functional disabling will involve this code anyway. Here is an example that uses the ReadOnly property, which in itself doesn't do anything in a ButtonCell:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
    if (cell.OwningColumn.CellType == typeof(DataGridViewButtonCell) 
    && cell.ReadOnly) Console.Write("This Cell is Disabled");
}

The syntax for setting it is for a sinlge cell or the whole column:

   ((DataGridViewButtonCell)dataGridView1[1, 1]).ReadOnly = true;
   dataGridView1.Columns[1].ReadOnly = true;

Note that after setting the whole column to ReadOnly you can't set a single Button back to ReadOnly = false !


You can visually indicate the disabled state by setting the ForeColor:

For one Cell:

((DataGridViewButtonCell) dataGridView1[1, 1]).Style.ForeColor = 
                                               SystemColors.InativeCaption;

Or for the whole Column:

   ((DataGridViewButtonColumn) dataGridView1.Columns[1]).DefaultCellStyle.BackColor =
                                                         SystemColors.InactiveCaption;

For these to work you need to set the Buttons' appearence to Flat, e.g.:

 ((DataGridViewButtonColumn)dataGridView1.Columns[1]).FlatStyle = FlatStyle.Flat;
Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
0

Try this,

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {           
     var dgv = (DataGridView)sender;                    
    if (dgv.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
    {    
    dgv.Rows[e.RowIndex].Cells["Recall"].ReadOnly = true;   
    }    
    }