Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

OpenFileDialog and backgroundworker

$
0
0

Hi!

I have a button from which I execute some background operation (reading txt file to datatable)

   Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        DataTable1.Clear()

       Dim openFileDialog1 As New OpenFileDialog()
        openFileDialog1.Filter = "TXT Documents (*.txt)|*.txt"
        openFileDialog1.InitialDirectory = IO.Directory.GetCurrentDirectory
        If openFileDialog1.ShowDialog() = DialogResult.OK Then

            selectedFile = openFileDialog1.FileName

            If Not BackgroundWorker1.IsBusy = True Then
                BackgroundWorker1.RunWorkerAsync()
            End If

        End If 

    End Sub

Backgroundworker is defined like this:

    Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        readFile(selectedFile)

    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

        BindingSource1 = New BindingSource
        BindingSource1.DataSource = DataTable1
        DataGridView1.DataSource = BindingSource1
   
    End Sub

So, after reading txt file to datatable I'm showing the content of datatable in datagridview.

When I open some file that has many rows (about 500 000) for the first time, all rows shows in datagridview in about 1-2 seconds. But if I go and open that file again than it slows down significantly and takes forever to show all rows.

The problem seems to be in line:

DataGridView1.DataSource = BindingSource1

When I comment that line than it goes well every time I open a file. I used DataTable1.Rows.Count property to confirm this (because when I comment that line mentioned before, I can't actually see the rows being populated in datagridview).

Why is it ok only for the first time I open the file and what should I do to repair this?


Viewing all articles
Browse latest Browse all 12583

Trending Articles