none
how to work around a bug in DataGridViewAutoFilter example code? RRS feed

  • Question

  • I found the auto filter example for the DataGridView at https://msdn.microsoft.com/en-us/library/Aa480727.aspx.  It does exactly what I need, but now I have found a bug and am not sure how to fix it.  The problem appears to be in the DataGridViewAutoFilterColumnHeaderCell class the dropDownListBox member is defined as a static.... that is ok as long as you always create the filtered dgv object from the same thread, once you change thread the next access of the filter list causes a cross thread access error.

    Has anyone seen that and figured out the solution??

    • Moved by Kristin Xie Wednesday, September 9, 2015 6:51 AM winform related
    Tuesday, September 8, 2015 6:57 PM

Answers

  • no, code is too big and proprietary stuff anyway.  a basic breakdown of one case kind of goes like this...

    in main gui.

    System.Threading.Thread wiz = new System.Threading.Thread(DoWiz);

    wiz.SetApartmentState(System.Threading.ApartmentState.STA);

    wiz.Start();

    // wait and keep checking for messages from other thread until it closes

    void DoWiz()

    {

    MyWizClass w = new MyWizClass();

    w.ShowDialog();

    }

    the MyWizClass is a form uses the datagridview with the auto filter code.  the first time it starts up it works fine.  if you close it and restart it the cross thread exception gets thrown when you try to use the filter.

    i tried the obvious thing, just took the static declaration out of the dropDownListBox definition... it seems to work, but i'm not sure if it is completely safe or why it would have been static in the first place if it doesn't need to be.  the only reason i can see is that you can only use one filter at a time so there is no need for multiple instances of it, but unless maybe you have big tables with lots of columns it shouldn't really hurt much.

    • Proposed as answer by Youjun TangModerator Monday, September 21, 2015 8:29 AM
    • Marked as answer by EPRI_Dave Monday, September 21, 2015 10:53 AM
    Thursday, September 10, 2015 2:13 PM

All replies

  • Hi EPRI_Dave,

    Based on your description, your question is more related to winforms controls. We have moved your thread to Windows Forms General forum, you will get more help there.

    Best Regards,
    Li Wang

    Wednesday, September 9, 2015 6:59 AM
  • Hi EPRI_Dave,

    Could you share some codes about how you use the multiple threads in your project? If it is possible, you could upload your project to OneDrive, it is better.

    It will help us to reproduce the issue and help you better.

    Regards,
    Youjun Tang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 10, 2015 6:53 AM
    Moderator
  • no, code is too big and proprietary stuff anyway.  a basic breakdown of one case kind of goes like this...

    in main gui.

    System.Threading.Thread wiz = new System.Threading.Thread(DoWiz);

    wiz.SetApartmentState(System.Threading.ApartmentState.STA);

    wiz.Start();

    // wait and keep checking for messages from other thread until it closes

    void DoWiz()

    {

    MyWizClass w = new MyWizClass();

    w.ShowDialog();

    }

    the MyWizClass is a form uses the datagridview with the auto filter code.  the first time it starts up it works fine.  if you close it and restart it the cross thread exception gets thrown when you try to use the filter.

    i tried the obvious thing, just took the static declaration out of the dropDownListBox definition... it seems to work, but i'm not sure if it is completely safe or why it would have been static in the first place if it doesn't need to be.  the only reason i can see is that you can only use one filter at a time so there is no need for multiple instances of it, but unless maybe you have big tables with lots of columns it shouldn't really hurt much.

    • Proposed as answer by Youjun TangModerator Monday, September 21, 2015 8:29 AM
    • Marked as answer by EPRI_Dave Monday, September 21, 2015 10:53 AM
    Thursday, September 10, 2015 2:13 PM
  • Hi,

    I think it's ok.

    Wednesday, September 16, 2015 8:17 AM