ProjectSMM.com
Gonzo TechNet
VB.Net

How to programatically add rows to a VB.Net DataGridView:
Note:Do NOT use DataGridView.NewRowIndex if AllowUserToAddRow is False.
When AllowUserToAddRows is False, NewRowIndex will ALWAYS be -1
dgvTargets.AllowUserToAddRows = FALSE
dgvTargets.Rows.Add()
With dgvTargets.Rows(dgvTargets.Rows.Count - 1)
    .Cells("FileName").Value = f.FullName
End With
How to list or enumerate Directory contents (files and directories)

As part of a project to analyse 50+ MS SQL servers, a colleague wrote a script to execute a PerfMon/SysMon script against each of the servers, outputting the statistics gathered to a disk file in .csv format that rolled each day at midnite. In the early morning, another script would then copy each of the files to a home system and deposit them in a directory structure of:
x:\PerfMon\<sysname>\<yymmdd>.csv

Using VB.Net (2008), I needed to enumerate/traverse through the directory tree searching for all the .csv files, load them into a DataGridView, and then select which ones would be read in and loaded into another SQL server for storage and analysis. I created a simple VB project, added a DataGridView to a form, set the AllowUserToAddRows = FALSE, and added two columns.

I then added a button called 'cmdLoadGrid', and set the onclick method to the following:

'Add this to the first line of the .VB FILE or just before the CLASS declaration
'so we have access to the assemblies that hold the filesystem functions

Imports System.io


Private Sub cmdLoadGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoadGrid.Click
	'--------------------------------------------------------------------------
	'Load the grid with a list of files or subfolders for the user to choose from
	'--------------------------------------------------------------------------
	'Delete all the rows from the datagrid
	Me.dgvFiles.Rows.Clear()
	'Call the sub that will traverse the sRoot path and load the files.
	Me.EnumDir("D:\Data")
       
End Sub

Private Sub EnumDir(ByVal sPath As String)
	'-------------------------------------------------------
	'This is a RECURSIVE (self-calling) sub.
	'Using the sPath passed, check each object within that directory.
	'If it is a SUB-Directory, call this sub and pass that directoryname
	'If it is a file, add it to the grid.
	'-------------------------------------------------------
	Dim oDir As New DirectoryInfo(sPath)

	For Each oFile As FileSystemInfo In oDir.GetFileSystemInfos
		'Is this a FILE or DIRECTORY?
		'"AND" the file object Attributes and test to see if the result = a Directory
		If (oFile.Attributes And FileAttributes.Directory) = FileAttributes.Directory Then
   			'It's a directory, recall this sub
			EnumDir(oFile.FullName)
		Else
			'It's a file
			'IF it's a .csv file, add it to the DataGridView
			If oFile.Extension = ".csv" Then
				dgvFiles.Rows.Add()
				'For the new row, set the values for each column
				With dgvFiles.Rows(dgvFiles.Rows.Count - 1)
					.Cells("Selected").Value = FALSE
					.Cells("FileName").Value = oFile.FullName
				End With
			End If
		End If
	Next
End Sub

Home | TechNet | ADO.Net | DOS | ASP.NET | IIS | VB.NET | VIM (vi) | Windows | XHtml
MS-SQL | T-SQL | SSIS | Oracle