Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Best approach for loading and handling file info in a Datagridview
 

Best approach for loading and handling file info in a Datagridview




What I'm attempting to do (with some success) is scan a directory and it's directories (no other directories below the child), and see if that file and path exist in a database by using .ExecuteScalar(). If 0 is returned then the file's record in the db has been deleted, and if it's 1 then it still exists.

While scanning the files and folders, I add the file info (file.Name, folder.name and file.Length) into a new row in the DataGridView, and add a check mark to the first column and color the row red if .ExecuteScalar() returns a 0. If 1 is returned then I keep the first column unchecked and color the row green.

When the user clicks on the Delete button, I want to be able to go through all of the rows, and if it's checked then delete that file and folder.

So far the scanning of the files and folders seem to be working, however with only 692Kb of test data (this is suppose to go through ~2 Terrabytes of data) in 39 files in 9 folders this already seems to be a bit sluggish, and takes about 3-4 seconds to load (and the vertical scrollbar loads up weird, is there a way to place a "disabled" greyed out scroll bar?).

My main question is what would be the best way to handle the scanning of files, populating the DatagridView and deleting the appropriate files afterwards? Should I store all of the info into an Array then load the Datagridview, and if the user checks or unchecks a row, then change the value in the array accordingly?

Below is the code that I'm using to scan the files/folders and check the db.

Any help would be greatly appreciated.

Thanks,
Jay

For Each folder In rootDir.GetDirectories
   For Each file In folder.GetFiles
      
Try
         
Dim cmd As New OleDbCommand
         cmd.CommandTimeout = 60
         cmd.Connection = conn
         cmd.CommandType = CommandType.Text
         
         sqlQuery =
"SELECT Count(Files.ID) FROM Files, Folders " _
         +
"WHERE Folders.Folder = '" + folder.Name.ToString + "' " _
         +
"AND Files.Folder = Folders.ID " _
         +
"AND Files.File = '" + file.Name + "';"

         
cmd.CommandText = sqlQuery
         conn.Open()

         If
conn.State = ConnectionState.Open Then
            
recordCount = cmd.ExecuteScalar()
         End If

         
Catch exp As Exception
            MessageBox.Show(exp.Message)
         
Finally

         
conn.Close()
      
End Try

      If recordCount > 0 Then
         dgFiles.Rows.Add(False, file.Name.ToString, folder.Name.ToString, (file.Length / 1024).ToString + "kb")
         Me.dgFiles.Rows(dgFiles.Rows.Count - 2).DefaultCellStyle.BackColor = colorOff
      Else
         
dgFiles.Rows.Add(True, file.Name.ToString, folder.Name.ToString, (file.Length / 1024).ToString + "kb")
         
Me.dgFiles.Rows(dgFiles.Rows.Count - 2).DefaultCellStyle.BackColor = colorOn
      
End If
   Next
Next

Carl J  Friday, November 11, 2005 5:40 PM
Check out the following sample. I think it would be a great framework for doing what you need:

http://www.windowsforms.net/TrackViews.aspx?ID=217&Type=Samples&Redir=%2fSamples%2fdownload.aspx%3fPageId%3d1%26ItemId%3d217%26tabindex%3d4

-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"
Mark Rideout  Saturday, November 12, 2005 12:27 AM

You can use google to search for other answers

Custom Search

More Threads

• Partial Class for TableAdapter
• C#2005 - UserControl in Datagridview
• How to create a multi-row gridview?
• DateTimePicker changes datasource only if date changes
• Null values being displayed
• Binding a control, not a datagridview, and a datagridview's controls to a dataset???
• Bind datagridviewimagecolumn to remote folder of images
• Error in auto code file behind XSD (Schema) file
• Reload the DataGrid
• IListSource breaks TextBox.Text databinding?