views:

181

answers:

2

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
 }
+1  A: 

I think you're problem is related to one I've experienced -- and asked about on SO:

http://stackoverflow.com/questions/1942782/datagridview-simultaneous-input-and-output-is-this-a-bug-in-datagridview

I hope you have more luck than me in coming up with a good solution.

Joe H
Ah geez, and I was hoping for a simple solution. :] Thanks!
joslinm
A: 

A sloppy solution for sure, but the only way I could make my cells editable again was to do a complete refresh.

private void DataGridViewCompleteRefresh()
        {
            dataGridView.DataSource = null;
            Thread.Sleep(100);
            dataGridView.DataSource = data.table;
            dataGridView.Columns["Site Origin"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView.Columns["Year"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView.Columns["Bitrate"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView.Columns["Release Format"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView.Columns["Bit Format"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView.Columns["Handled"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView.Columns["Error"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView.Columns["File Path"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView.Update();
            dataGridView.Refresh();
        }
joslinm