Change in type of datagridview cell from textbox to CheckBox RRS feed

  • Question

  • I am trying to change the cell type from DataGridViewTextBoxCell to DataGridViewCheckBoxCell @ runtime as below:



    DataGridViewCheckBoxCell chkBoxCell = new DataGridViewCheckBoxCell();

    FeatureGridView[1, 1] = chkBoxCell;


    However, the application crashes everytime while painting the control giving the following error:


    DataGridView Default Error Dialog



    The following exception occurred in the DataGridView: System.FormatException: Formatted value of the cell has a wrong type. To replace this default dialog please handle the DataError event.

    Can anyone help me in resolving the error ?


    Thursday, November 20, 2008 12:13 PM


  • Your problem is going to be that the value it is going to use is a string and not a boolean.  You could probably go from a checkbox to a textbox just fine.  Still I don't think dynamically changing columns is a good way to go.  You'd have to write a bunch of conditional code to work with the columns.  This doesn't even get into the problems you'll have getting data into the columns and setting the data back into the data source.

    I think a better option is to add additional columns that are initially hidden.  You can then hook them all up appropriately and then at runtime toggle the visibility as needed.  This is how a lot of UIs work such as expanding dialogs and it works really well.

    Michael Taylor - 11/20/08
    • Marked as answer by Zhi-Xin Ye Tuesday, November 25, 2008 3:38 AM
    Thursday, November 20, 2008 2:31 PM