Jax Teller
Jax Teller

Reputation: 173

DataTable in DataGridView via DataSource starts duplicating data after next call

I have the following code snippet in a backgroundWorker_RunWorkerCompleted method. It all works perfectly, data that I am expecting shows in the DataGridView.

Updated code:

    private void GetResults()
    {
        dataGridView1.DataSource = null;

        DataTable errorTable= new DataTable();
        errorTable.Columns.Add("Desc", typeof(string));
        errorTable.Columns.Add("Line", typeof(int));
        errorTable.Columns.Add("File", typeof(string));

        foreach (var filePath in relPaths)
        {
            SyntaxCheck sc = new SyntaxCheck
            {
                filePath = singlePath
            };

            errorTable.Merge(sc.searchFiles(), true);
        }

        dataGridView1.DataSource = errorTable;
    }

GetResults() is called in backgroundWorker_RunWorkerCompleted()

As soon as I press a button that does all this again, my DataGridView updates and has 2 of each result. I do it again and it has 3 of each result. I've been going through my code for a couple hours and cannot find the error.

It looks like it just keeps appending data in the merge. If I edit my data in the files so that they shouldn't appear in the DataTable, and then press the button again, all the entries for that data are gone. Same behaviour as before keeps going on for the other data though.

All the DataTables have the same columns. I've tried calling Rows.Clear() at various points, changes nothing.

Upvotes: 0

Views: 50

Answers (1)

jdweng
jdweng

Reputation: 34421

If you add break points into the code and then hover over errorTable you will see a down arrow and then selecting Datatable Visualizer you will see the contents of the table. So you can isolate where the problem by placing break points in different sections of the code until you find where the duplicates are being added.

Upvotes: 1

Related Questions