none
Can't use a CheckBox coumn on DataGridView bound to strongly typed DataSet with Boolean column

    Question

  • Hello all,

    We have a strongly typed dataset with a table column defined as boolean. The table is bound to a DataGridView which has columns defined to match the datatable. If we try to make the boolean DataGridView column a CheckBox column, it compiles fine but when the application is run, it throws an error in the Application OnCreateMainForm method. The error is "Unable to cast object of type 'System.Windows.Forms.DataGridViewCheckBoxColumn' to type 'System.Windows.Forms.DataGridView TextBoxColumn'." Can someone point me on this? Thanks in advance for any ideas and/or suggestions!

    Thursday, April 15, 2010 5:23 PM

Answers

  • Thanks Vladimir. I found it. In two different places I was using a more detailed collection than needed. Once I changed DataGridViewTextBoxColumn to DataGridViewColumn in the following code, it worked.

    For Each column As DataGridViewTextBoxColumn In MyDataGridView.Columns 
        column.Width = column.GetPreferredWidth( _
            DataGridViewAutoSizeColumnMode.AllCells, True)
    Next

    For Each column As DataGridViewColumn In MyDataGridView.Columns
        column.Width = column.GetPreferredWidth( _
            DataGridViewAutoSizeColumnMode.AllCells, True)
    Next

    I also had to change the following code from DataGridViewTextBoxCell to DataGridViewCell.

    For Each cell As DataGridViewTextBoxCell In row.Cells
        serverRow.Item(cell.ColumnIndex) = cell.Value
    Next

    For Each cell As DataGridViewCell In row.Cells
        serverRow.Item(cell.ColumnIndex) = cell.Value
    Next

     

    • Marked as answer by j2associates Friday, April 16, 2010 1:17 AM
    Friday, April 16, 2010 1:12 AM

All replies

  • Hello j2associates,

    that's a very strange behavior you described ... I couldn't reproduce it myself. 

    Try debugging it to see what's going on in runtime at the moment the exception is thrown and whether the code is generated correctly.
    BTW, are you using some custom DataGridView columns ?  Or have you changed the code in the form designer file manually ?

    In case you're (or someone else is) having trouble locating the actual line that causes the exception, you need to do the following:

    1. In Solution Explorer, select project node or any node within the project hierarchy, and click on the "Show All Files" button.

    2. Expand the main Form node and open Form.Designer.vb file.

    3. Locate InitializeComponent method and remove the DebuggerStepThrough attribute:

      '<System.Diagnostics.DebuggerStepThrough()> _
      Private Sub InitializeComponent()
    

    NOTE: Make sure you don't open and edit the form in designer, since it will regenerate Designer.vb file and all changes you made manually changes will be lost.

    4. Put a break point at the start of InitializeComponent method and start debugger.

     

    Now you should have a better insight on why the exception is thrown.

    This is in general how you can debug InitializeComponent method in VB.Net (as opposed to c# where you don't have to go through all these extra steps).
    In this case you can actually see the exact line of the InitilaizeComponent where the exception is thrown in the StackTrace of the InnerException (of the exception reported by the IDE), so you can set the break point directly to that line and inspect it at runtime (in case it's not obvious when you look at the code).

     

    If you get stuck with this one, please post the form's InitializeComponent method and the form's constructor, if you implemented one, so we can try to reproduce the issue.

    best regards,
    Vladimir

    Thursday, April 15, 2010 8:17 PM
  • Hello Vladimir,

    I manually added the DataGridView columns at design time by clicking the ellipses in the Columns property to get the default collection editor. I have one for each column in my stronly typed dataset. The dataset is read from an Xml file using DataSet.ReadXml(path, XmlReadMode.InferSchema). The Xml file was saved using DataSet.WriteXml(path, XmlWriteMode.IgnoreSchema). All of the Xml tags pertaining to the column in question are lower case true and false values. I'll try your debugging suggestion and report back. Thanks for taking the time to respond!

     

    Thursday, April 15, 2010 9:50 PM
  • Thanks Vladimir. I found it. In two different places I was using a more detailed collection than needed. Once I changed DataGridViewTextBoxColumn to DataGridViewColumn in the following code, it worked.

    For Each column As DataGridViewTextBoxColumn In MyDataGridView.Columns 
        column.Width = column.GetPreferredWidth( _
            DataGridViewAutoSizeColumnMode.AllCells, True)
    Next

    For Each column As DataGridViewColumn In MyDataGridView.Columns
        column.Width = column.GetPreferredWidth( _
            DataGridViewAutoSizeColumnMode.AllCells, True)
    Next

    I also had to change the following code from DataGridViewTextBoxCell to DataGridViewCell.

    For Each cell As DataGridViewTextBoxCell In row.Cells
        serverRow.Item(cell.ColumnIndex) = cell.Value
    Next

    For Each cell As DataGridViewCell In row.Cells
        serverRow.Item(cell.ColumnIndex) = cell.Value
    Next

     

    • Marked as answer by j2associates Friday, April 16, 2010 1:17 AM
    Friday, April 16, 2010 1:12 AM