Gabor
Gabor

Reputation: 1676

.NET 3.5 WinForms - DataGridView crashes on refresh(). Is it a bug?

This is a multi threaded scenario.

  1. The main thread handles the application and UI events, and it starts up a new thread to do some background operations.
  2. The "background" thread loads the data from files into a data-table of a strongly-typed dataset. The DataGridView is bound to that DataTable.
  3. Once the data is ready, the "background" thread invokes the refresh() function of the DataGridView on the form.

If there are more lines then what fits on one screen and the vertical scrollbar is to appear: the grid crashes. The new datalines are always displayed. Error only occurs if there are enough lines to display the scrollbar (see image below).

Sample image of DataGridView crash

I use .NET 3.5. In Windows XP it crashes the whole application. On Win 7 (64 bit) only the grid becomes unresponsive, but once I resize the window the scrollbar appears and all is fine.

The relevant parts of the code are attached below.

Grid refresh operation in the form's .cs file:

    public void ThreadSafeRebindGrids()
    {
        SimpleCallBack callBackHandler = new SimpleCallBack(RebindGrids);
        this.BeginInvoke(callBackHandler);
    }
    public void RebindGrids()
    {
        gridCurrentResults.Refresh(); // The problematic DataGridView refresh()
        gridAllResults.Refresh();
    }
    public delegate void SimpleCallBack();

The update part in the "background" thread:

    void Maestro32_SampleFinished(object sender, MeasurementEvents.SampleFinishedEventArgs e)
    {
        //--- Read new results
        ParentForm.ThreadSafeSetStatusInfo("Processing results for sample no. " + e.SampleNo.ToString() + "...");
        CurrentMeasurement.ReadSpeResults();   // Updating the DataTable in the strongly typed DataSet (see below)
        ParentForm.ThreadSafeRebindGrids();    // Refresh the DataGridView
        ParentForm.ThreadSafeRefreshNumbers();
    }

The objects related to the "background" thread have a direct reference to the DataSet (UiDataSource). The DataTable (CurrentSamples) is updated in the following manner:

    /// <summary>
    /// Adds a new sample to the CurrentSamples table of the UiDataSet.
    /// </summary>
    /// <param name="sample">The new sample to be added to the table.</param>
    /// <param name="serial">The serial number of the sample being added</param>
    private void AddSampleToCurrentResults(SampleData sample, int serial)
    {
        UiDataSource.CurrentSamples.AddCurrentSamplesRow(serial,
                                                       sample.MeasurementDate,
                                                       (uint)Math.Round(sample.SampleCountSum),
                                                       true, //--- Set the checkbox checked
                                                       sample.LiveTime,
                                                       sample.RealTime);
    }

DataGridView options:

        // 
        // gridCurrentResults (generated)
        // 
        this.gridCurrentResults.AllowUserToAddRows = false;
        this.gridCurrentResults.AllowUserToDeleteRows = false;
        this.gridCurrentResults.AllowUserToOrderColumns = true;
        this.gridCurrentResults.AllowUserToResizeRows = false;
        this.gridCurrentResults.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.gridCurrentResults.AutoGenerateColumns = false;
        this.gridCurrentResults.CausesValidation = false;
        this.gridCurrentResults.ColumnHeadersHeight = 25;
        this.gridCurrentResults.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.selectedCol,
        this.SampleNoCol,
        this.MeasuredValueCol,
        this.liveTimeCol,
        this.realTimeDataGridViewTextBoxColumn,
        this.AtTimeCol});
        this.gridCurrentResults.DataMember = "CurrentSamples";
        this.gridCurrentResults.DataSource = this.uiDataSource;
        this.gridCurrentResults.Location = new System.Drawing.Point(11, 24);
        this.gridCurrentResults.Margin = new System.Windows.Forms.Padding(8);
        this.gridCurrentResults.Name = "gridCurrentResults";
        this.gridCurrentResults.RowHeadersVisible = false;
        this.gridCurrentResults.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        this.gridCurrentResults.ShowEditingIcon = false;
        this.gridCurrentResults.Size = new System.Drawing.Size(534, 264);
        this.gridCurrentResults.TabIndex = 0;
        this.gridCurrentResults.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.gridCurrentResults_CellContentClick);

If I made a mistake somewhere please point it out to me.

@ChrisF:

I tried removing the refresh() statement, as I am doing pretty much the same what u suggested. The only difference is the databinding, it looks like:

this.dataGridView.DataSource = this.dataSet;
this.dataGridView.DataMember = "dataTable";

And I update the dataTable in a similar way, but from another thread.

But the new data lines do not appear until I, say, resize the window.

Which raises the question how I can properly update the dataTable from another thread?

Upvotes: 2

Views: 5123

Answers (2)

drharris
drharris

Reputation: 11214

I'm guessing the problem has to do with how WinForms works inside the STA model for threading. Basically, the DataTable you're accessing is located somewhere, and that is probably inside the form we see above. So, when you update the DataTable from another thread, which thread gets the events needed for binding? Likely the thread you update it from, and the form's thread is not aware of the changes being made. So, you simply need to invoke any calls to DataTable onto the form itself, so it receives the events properly:

this.Invoke(() => {
   // any calls involving DataTable
});

It seems backwards, but keep in mind in an "enterprise" situation, you'd probably be accessing that dataset by multiple adapters. So, your update thread would have an adapter to itself, and your GUI would have its own also. The other solution would be to use a BindingList, which I believe has thread compatibility for this type of situation, but don't quote me on that.

For extra credit, this could also explain your problem before with crashing. By accessing the DataGridView from the background thread, you had cross-thread operations going on.

Upvotes: 4

ChrisF
ChrisF

Reputation: 137178

I wouldn't call:

    gridCurrentResults.Refresh(); // The problematic DataGridView refresh()
    gridAllResults.Refresh();

These will take progressively longer and longer as the data set gets larger and larger.

I've written an application that uses a DataGridView to display mp3 file information. I set the DataSource of the DataGridView to a DataTable:

this.dataGridView.DataSource = this.dataTable;

and then simply add the new information to the DataTable:

this.dataTable.Rows.Add(row);

This automatically updates the DataGridView.

Upvotes: 0

Related Questions