Elwood472
Elwood472

Reputation: 37

How to show records added to datagridview in real time

A co-worker needs to search our network and her File Explorer search does not work well. I threw this app together quickly to allow her to search and it works well. The results are written to a datagridview, but the results are not shown until the search is complete.

I would like the datagridview to show records as they are added and allow her to cancel the search if she wants.

Using a backgroundworker, I tried to refresh the grid, but as soon as it finds a match, the code stops running. There are no errors, it just stops running.

So how can I get the grid to update as it continues to search?

Public dtResults As DataTable
Dim myDataSet As New DataSet
Dim myDataRow As DataRow

Dim colType As DataColumn
Dim colResult As DataColumn

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    dtResults = New DataTable()

    colType = New DataColumn("Type", Type.GetType("System.String"))
    colResult = New DataColumn("Search Result", Type.GetType("System.String"))

    dtResults.Columns.Add(colType)
    dtResults.Columns.Add(colResult)

    DataGridView1.DataSource = dtResults
    DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill


End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click

    btnSearch.Enabled = False
    sbStatusBar.Text = "Searching..."
    dtResults.Clear()

    BackgroundWorker1.RunWorkerAsync()

End Sub

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

    LoopSubFolders(txtSearchLocation.Text)

End Sub
Public Sub LoopSubFolders(sLocation As String)

    Dim di = New DirectoryInfo(sLocation)
    Dim mySearchterm As String = LCase(txtSearchTerm.Text)
    Dim fiArr As FileInfo() = di.GetFiles()
    Dim sSearchTarget As String

    sbStatusBar.Text = "Searching " & sLocation
    'Search File names in 
    If cbFileNames.Checked = True Then
        For Each myFile In fiArr
            sSearchTarget = LCase(myFile.Name)
            If sSearchTarget.Contains(mySearchterm) Then

                myDataRow = dtResults.NewRow()
                myDataRow(dtResults.Columns(0)) = "File"
                myDataRow(dtResults.Columns(1)) = Path.Combine(sLocation, myFile.Name)
                dtResults.Rows.Add(myDataRow)

            End If
        Next
    End If

    For Each d In Directory.GetDirectories(sLocation)
        If cbFolderNames.Checked = True Then
            sSearchTarget = LCase(d)
            If sSearchTarget.Contains(mySearchterm) Then

                myDataRow = dtResults.NewRow()
                myDataRow(dtResults.Columns(0)) = "Folder"
                myDataRow(dtResults.Columns(1)) = d
                dtResults.Rows.Add(myDataRow)

            End If
        End If

        LoopSubFolders(d)

    Next

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

    btnSearch.Enabled = True
    sbStatusBar.Text = "Complete"
    DataGridView1.DataSource = Nothing
    DataGridView1.DataSource = dtResults
    DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

End Sub

Upvotes: 0

Views: 760

Answers (2)

user10216583
user10216583

Reputation:

Variables

Well, let's start from the top with some class level variables:

'Notice the enabled properties.
Private WithEvents BackgroundWorker1 As New BackgroundWorker With {.WorkerReportsProgress = True, .WorkerSupportsCancellation = True}
'To monitor the cancellation, set by the Cancel Button.
Private bgwCancel As Boolean = False
'The DGV source.
Private dtResults As New DataTable
'The start directory.
Private startDir As String
'The search keyword.
Private searchWord As String
'Whether to search the sub directories, from a check box for example.
Private includeSubDirectories As Boolean = True
'Whether to search the files, from another check box.
Private includeFiles As Boolean = True

The Constructor

Prepare your DGV and whatever else you need here.

Sub New()
    dtResults.Columns.Add(New DataColumn("Type", Type.GetType("System.String")))
    dtResults.Columns.Add(New DataColumn("Search Result", Type.GetType("System.String")))

    DataGridView1.DataSource = dtResults
    DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

    'Make sure you add the image column after binding the data source.
    Dim imgCol As New DataGridViewImageColumn(False)

    With imgCol
        .Image = Nothing
        .Name = "imgCol"
        .HeaderText = ""
        .Width = 50
        .DefaultCellStyle.NullValue = Nothing
    End With
    DataGridView1.Columns.Insert(0, imgCol)
End Sub

Iterator

Now, let's write the search routine. I'd do that through an Iterator function:

Private Iterator Function IterateFolders(startDir As String, includeFiles As Boolean, includeSubDir As Boolean) As IEnumerable(Of String)
    For Each dirName In IO.Directory.EnumerateDirectories(startDir)
        Yield dirName

        If includeFiles Then
            For Each fileName In IO.Directory.EnumerateFiles(startDir)
                Yield fileName
            Next
        End If

        If includeSubDir Then
            For Each subDir In IterateFolders(dirName, includeFiles, includeSubDir)
                Yield subDir
            Next
        End If
    Next
End Function

The Main Thread Updater

A routine called by the worker's thread to update the DataTable and any control that belongs to the main thread:

Private Sub AddSearchResult(path As String)
    If InvokeRequired Then
        Invoke(Sub() AddSearchResult(path))
    Else
        dtResults.Rows.Add(If(IO.File.Exists(path), "File", "Folder"), path)
        sbStatusBar.Text = $"Searching {path}" 
    End If
End Sub

Start

In the click event of the start button, do the necessary validations, assign the values to their variables, and start the back ground worker:

If String.IsNullOrEmpty(txtSearchKeyword.Text) Then Return
If String.IsNullOrEmpty(txtSearchLocation.Text) Then Return

bgwCancel = False
dtResults.Rows.Clear()
startDir = txtSearchLocation.Text
searchWord = txtSearchKeyword.Text.ToLower
includeSubDirectories = chkIncludeSubDirs.Checked
includeFiles = chkFiles.Checked
btnSearch.Enabled = False
sbStatusBar.Text = "Searching..."
BackgroundWorker1.RunWorkerAsync()

Cancel

To cancel the search, in the click event of the cancel button I presume, True the bgwCancel variable:

bgwCancel = True

The BackgroundWorker - DoWork

Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    For Each item As String In IterateFolders(startDir, includeFiles, includeSubDirectories)
        If bgwCancel Then
            BackgroundWorker1.CancelAsync()
            Return
        End If
        If item.ToLower.Contains(searchWord) Then
            AddSearchResult(item)
        End If
        Threading.Thread.Sleep(100)
    Next
End Sub

Note that, Its good practice to give a lengthy routine a BREATH through the Sleep(ms) method of that thread.

The BackgroundWorker - ProgressChanged

I don't think you need it here.

The BackgroundWorker - RunWorkerCompleted

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    If bgwCancel Then
        sbStatusBar.Text = "Canceled!"
        MessageBox.Show("Canceled by you!")
    ElseIf e.Error IsNot Nothing Then
        sbStatusBar.Text = "Error!"
        MessageBox.Show(e.Error.Message)
    Else
        sbStatusBar.Text = "Complete"

        'YOU DO NOT NEED TO DO THIS. Remove the following
        'DataGridView1.DataSource = Nothing
        'DataGridView1.DataSource = dtResults
        'DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    End If
    btnSearch.Enabled = True
End Sub

The Image Column

Handle the RowsAdded event of the DGV as follow:

Private Sub DataGridView1_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
    If DataGridView1.Columns.Count < 3 Then Return

    'if you want to get rid of the default x image.
    If e.RowIndex = 0 Then
        DataGridView1.Rows(e.RowIndex).Cells("imgCol").Value = Nothing
    End If

    Dim path As String = DataGridView1.Rows(e.RowIndex).Cells(2).Value?.ToString

    If Not String.IsNullOrEmpty(path) Then
        If IO.File.Exists(path) Then
            DataGridView1.Rows(e.RowIndex).Cells("imgCol").Value = Icon.ExtractAssociatedIcon(path).ToBitmap
        Else
            DataGridView1.Rows(e.RowIndex).Cells("imgCol").Value = My.Resources.Folder
        End If
    End If
End Sub

Where the My.Resources.Folder is an icon file of your choice for the folder entries.

Good luck.

Upvotes: 0

jmcilhinney
jmcilhinney

Reputation: 54457

Here's an example of how you might do it using the suggested ReportProgress method and ProgressChanged event:

Private table As New DataTable

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Configure table here.

    DataGridView1.DataSource = table
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Setup UI here.

    'Note that you MUST pass in the TextBox data as you MUST NOT touch the UI directly on the secondary thread.
    BackgroundWorker1.RunWorkerAsync({TextBox1.Text, TextBox2.Text})
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    'Get the data passed in and separate it.
    Dim arguments = DirectCast(e.Argument, String())
    Dim folderPath = arguments(0)
    Dim searchTerm = arguments(1)

    SearchFileSystem(folderPath, searchTerm)
End Sub

Private Sub SearchFileSystem(folderPath As String, searchTerm As String)
    For Each filePath In Directory.GetFiles(folderPath)
        If filePath.IndexOf(searchTerm, StringComparison.InvariantCultureIgnoreCase) <> -1 Then
            'Update the UI on the UI thread.
            BackgroundWorker1.ReportProgress(0, {"File", filePath})
        End If
    Next

    For Each subfolderPath In Directory.GetDirectories(folderPath)
        If subfolderPath.IndexOf(searchTerm, StringComparison.InvariantCultureIgnoreCase) <> -1 Then
            'Update the UI on the UI thread.
            BackgroundWorker1.ReportProgress(0, {"Folder", subfolderPath})
        End If

        SearchFileSystem(subfolderPath, searchTerm)
    Next
End Sub

Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    'Get the data passed out and separate it.
    Dim data = DirectCast(e.UserState, String())

    'Update the UI.
    table.Rows.Add(data)
End Sub

Note that you should NEVER touch the UI directly in the DoWork event handler or a method called from it. ONLY touch the UI on the UI thread. That means that the text in your TextBoxes must be extracted BEFORE calling RunWorkerAsync. You can eithewr pass the Strings in as arguments or you can assign them to fields and access them from there on any thread. Don't EVER access a member of a control on other than the UI thread. Some times it will work, sometimes it will appear to work but not do as intended and sometimes it will crash your app. So that you don't have to remember which specific scenarios cause which result, avoid such scenario altogether.

I haven't tested this code so I'm not sure but you may have to call Refresh on the grid or the form after adding the new row to the DataTable.

Upvotes: 1

Related Questions