[C#]fill a datagridview from a secondary thread

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akshaycjoshi
    New Member
    • Jan 2007
    • 153

    [C#]fill a datagridview from a secondary thread

    I have one datagridview which i fill using the connected verion of ADO.NET.
    When the user presses the search button a seperate thread is created and in that thead i connect to the database and fill the datagridview.


    Code:
            private void cmdsearch_Click(object sender, EventArgs e)
            {
                ThreadStart t = fillthegrid;
                Thread fillthread = new Thread(t);
                fillthread.Start();
            }
            
            void fillthegrid()
            {
                //do some stuff like disabling the search button to avoid repetitive calls
                //here I connect to the database and show it in datagridview
                //enabling the search button again
            }
    In the fillthegrid() I will have acces the button.enable property and access the the rows for writing but since the thread is not the primary I cant access it.
    How to get around this problem ?

    Thanks !
    Last edited by Curtis Rutland; Oct 21 '08, 04:41 PM. Reason: please use [CODE] tags when posting code.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Create a new Thread class whose constructor takes a grid and use that.

    Comment

    • akshaycjoshi
      New Member
      • Jan 2007
      • 153

      #3
      And what about the button ?
      Also, since the thread is the not the one which created the Grid will I be able to access it is suspecious.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by akshaycjoshi
        And what about the button ?
        Also, since the thread is the not the one which created the Grid will I be able to access it is suspecious.
        If your thread takes a grid as argument then it doesn't matter who created it. The thread should be able to work on that grid just fine.
        A similar approach should suffice for the button. If the thread needs to update many more controls though then a different approach would be required.

        Comment

        • akshaycjoshi
          New Member
          • Jan 2007
          • 153

          #5
          Please explain that method by an example as I need to update many controls from a secondary thread.

          Comment

          • akshaycjoshi
            New Member
            • Jan 2007
            • 153

            #6
            bump ! (to get it on top again)

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by akshaycjoshi
              Please explain that method by an example as I need to update many controls from a secondary thread.
              If you need to update many controls then make that thread an inner class in the class that has those controls.

              Comment

              Working...