DGVComboBoxColumn gives ThreadStateException

I'm having a problem when selecting cells in a DataGridViewComboBoxColumn.  Each time a cell is selected I see the following error:
"DataGridView Default Error Dialog:
System.Threading.ThreadStateException: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made.  Ensure that you Main function has STAThreadAttribute marked on it. ..."

Firstly, I don't use any Main() methods in my application.  I did try adding one with the suggested attribute, but it had no affect.  Does anyone know what this means
Thanks,
Nate


Answer this question

DGVComboBoxColumn gives ThreadStateException

  • Dheerendra

    In VB, double-click the "My Project" icon in the solution explorer and tell me if the "Enable application framework" check box is checked. If it is unchecked, check it and then set your Startup form value appropriately and see if that helps. < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    -mark

    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"

     


  • hYp3r57231

    Yes - look like a hosting issue. Visual Studio creates a hosting environment around the application. Looks like there was some problems getting that installed. In the mean time you can also goto the project properties and in the Debugger tab uncheck the "Enable the Visual Studio hosting process" and this should be just the same as double-clicking your EXE.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    -mark

    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"

     

     

  • naaz911

    I went ahead and phased out the need for the Module and set the startup form as my main MDI form.  I still have the Module around for Public Const variable needs.  I checked "Enable application framework" and Window styles are now being applied.  Thank you for that.
    I'm still seeing the DGVComboBoxColumn exception.
    -Nate

  • Lana567890

    yeah, sorry, VB .Net 2.0.
    I have a large application converted from VS '03 to '05 with many Forms and derived Forms, all of which are opened in one main MDI window via a MainMenu.  It would be impossible to post code due to the size of the app.  I created a sample application with the basic functionality and I don't see the exception.
        As far a the Main() method goes, I don't include it in any of my Form classes.  It seemed from the error message that I might need this method in at least the form with the ComboBoxColumn control.  So I tried to add Main() with STAThreadAttribute, but this had no effect.
        I noticed on the MSDN2 page for DGVComboBoxColumn that they included Main() for enabling Visual styles.  I also have a problem where all of my controls are plain 3D boxes with no XP Styles.  In the same sample application I tried for the above I see XP styleing applied.  I didn't change any features, so I'm not sure why they aren't being drawn this way as well.
    I'm not really sure where do go from here.
    Thanks,
    Nate

  • Rainz

    Just found something strange.  The other two developers in my group do not see the exception on their machines.  And our project configurations are the same.  We all link to the same files in SourceSafe.  Could I have an installation problem with VS 2005
    -Nate

  • Thomas Logan

    This can also occur if you're updating your control from the incorrect thread. 

    Check the callstack where you're getting this exception to see where you're currently calling into the DGV from using Debug->Windows->Callstack. If you're on the incorrect thread, you need to call DataGridView.Invoke or BeginInvoke to call that method.

  • SimonSa

    Another note:
    After clicking OK for the error the ComboBox does show up and is available for use.  But, the value set in the ComboBox does not match the Cells current value.  The combobox's value starts with the first value in the list.  And each cell that is clicked on after that the combobox value is the last selection made.  The value of the combobox does not get updated to match the cells value.
    -Nate

  • Dougins

    Firstly, thanks for your post, but who has marked your post as the answer to this thread
    Because it hasn't answered my problem yet and I don't want to deter other people from posting.

    The CallStack is empty when this error occurs.  This is not an area that i am doing anything programatically.  When I manually click on a cell this error occurs.  I don't understand what you mean for me to use DataGridView.Invoke for.  Was this a reference to my problem with trying to use the Main() method in a Form


  • sacuza

    If you can post some code or describe your application structure a bit it will help us identify what is going on. Also, when you say you don't have a main() I assume you are using VB If so, we can try some things out to create a custom main routine if need be.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    thanks,

    -mark

    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"

     


  • Rob Hershfield

    I unchecked "Enable the VS hosting process" and was able to click on the ComboBoxes without the thread exception.  I also tried to Repair my installation of Visual Studio, but this had no affect on the problem.  Do you think I should do a complete uninstall/reinstall of the application
    -Nate

  • dirtyKarl

    I'm using a Module as the startup class in my main project.  This module initializes and launches my main MDI form.

  • Matthew Martin

    I thought of this earlier but got sidetracked with another issue.  I just tried running the .EXE directly and I Did Not see the exception.  So, what do you think, re-install VS
    Thanks,
    Nate

  • J. Leite

    I tried an unintall/reinstall of VS and I unfortnuately still see the problem.  If I create a sample application however, I do Not see the problem.  Could I still have a configuration problem that would differ from my coworkers, something that wouldn't necessarily be a file that gets checked into SourceSafe
    Thanks,
    Nate

  • Meffistas

    It does sound as if there is a configuration issue going on here. What if you launch your application from the command line/explorer instead of inside of visual studio Just double-click the EXE. Same issue < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    -mark

    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"

     


  • DGVComboBoxColumn gives ThreadStateException