DataGridView cells not editable when using an outside thread call

Posted by joslinm on Stack Overflow See other posts from Stack Overflow or by joslinm
Published on 2010-04-25T00:05:32Z Indexed on 2010/04/25 0:13 UTC
Read the original article Hit count: 193

Filed under:
|
|

Hi, I'm not able to edit my datagridview cells when a number of identical calls takes place on another thread. Here's the situation:

  • Dataset table is created in the main window
  • The program receives in files and processes them on a background thread in class TorrentBuilder : BackgroundWorker creating an array objects of another class Torrent
  • My program receives those objects from the BW result and adds them into the dataset

The above happens either on my main window thread or in another thread:

I have a separate thread watching a folder for files to come in, and when they do come in, they proceed to call TorrentBuilder.RunWorkerAsynch() from that thread, receive the result, and call an outside class that adds the Torrent objects into the table.

When the files are received by the latter thread, the datagridview isn't editable. All of the values come up properly into the datagridview, but when I click on a cell to edit it: I can write letters and everything, but when I click out of it, it immediately reverts back to its original value. If I restart the program, I can edit the same cells just fine.

If the values are freshly added from the main window thread, I can edit the cells just fine.

The outside thread is called from my main window thread, and sits there in the background. I don't believe it to be ReadOnly because I would have gotten an exception.

Here's some code:

From my main window class:

private void dataGridView_DragDrop(object sender, DragEventArgs e)
{
    ArrayList al = new ArrayList();
    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

    foreach (string file in files)
    {
      string extension = Path.GetExtension(file);
      if (Path.GetExtension(file).Equals(".zip") || Path.GetExtension(file).Equals(".rar"))
      {
          foreach (string unzipped in dh.UnzipFile(file))
          al.Add(unzipped);
      }
      else if (Path.GetExtension(file).Equals(".torrent"))
      {
          al.Add(file);
      }
    }

dataGridViewProgressBar.Visible = true;
tb.RunWorkerCompleted += new RunWorkerCompletedEventHandler(tb_DragDropCompleted);
tb.ProgressChanged += new ProgressChangedEventHandler(tb_DragDropProgress);
tb.RunWorkerAsync()
}

void tb_DragDropCompleted(object sender, RunWorkerCompletedEventArgs e)
{
   data.AddTorrents((Torrent[])e.Result);
   builder.Dispose();
   dh.MoveProcessedFiles(data);
   dataGridViewProgressBar.Visible = false; 
}

From my outside Thread

while (autocheck)
{
   if (torrentFiles != null)
   {
     builder.RunWorkerAsync(torrentFiles);
     while (builder.IsBusy)
     Thread.Sleep(500);
   }
}
void builder_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
      data.AddTorrents((Torrent[])e.Result);
      builder.Dispose();
      dh.MoveProcessedFiles(xml);
      data.Save(); //Save just does an `AcceptChanges()` and saves to a XML file
 }

© Stack Overflow or respective owner

Related posts about multithreading

Related posts about c#