我创建了一个应用程序,它正在读取工作站的DNS名称并对其进行ping操作,以查看是否有任何工作站存在网络连接问题。
一切都很好,直到最后,我正在尝试读取输出文件信息并将其显示在DataGridView
(DGV)中。
当我尝试从读取的文本文件中显示创建的数据表的代码并填充不同的DataGridView时,会出现问题。选择一个房间,它工作正常。但是当我尝试将数据输出到多个DataGridViews时,只有 last DataGridView填充了它的数据。之前的DGV都没有填充。
更多信息,我有一个带有TabPages的TabControl,对应于每个房间,在这些TabPages中是每个单独的DataGridView所在的位置。所以总共有12个DataGridViews 可以需要为其数据源分配数据表信息。
我尝试过简单地通过DGV.Rows.Add()添加数据[这不是实际代码,只是一个解释]。现在虽然这可以通过单个选择将信息输出到DataGridView,但是当我简单地向其添加行数据时,我找不到清除DataGridView的方法。 DGV<Name>.Rows.Clear()
没有做任何事,DGV<Name>.Rows.Remove(#)
错误,DGV<Name>.DataSource = VbNull
也没有做任何事情,我假设那是因为使用这种方法我实际上并没有在DGV中添加数据源
为了确保每个房间的DGV在其适当的DGV中正确输出信息,我需要做些什么的帮助。非常感谢。
我正在使用VS 2015社区版,如果这有任何区别的话。我无法访问LINQ插件,只是在案例中人们认为这可能是解决它的方法。
以下是相关代码:
Private Sub loadResults()
Dim i As Integer = 0 'Counter variable
Dim fName As String() = GlobalVariables.selectedRoomsList.ToArray
Dim x As Integer = fName.Length 'Upper limit of the Array to be checked against
Dim AppDir As String = Path.GetDirectoryName(Path.GetDirectoryName(Application.StartupPath))
Dim dataName As String = "dgv" & fName(i)
Dim resultFilePath As String = AppDir & "\data\output\" & fName(i) & ".txt"
Dim fileExists As Boolean = File.Exists(resultFilePath)
Dim rowvalue As String
Dim cellvalue(5) As String
Dim oDataTable As DataTable
lblGoodPingTotal.Text = GlobalVariables.successfulPings
lblBadPingTotal.Text = GlobalVariables.failedPings
lblTimeElapsed.Text = GlobalVariables.elapsedTime
Do Until i > x
oDataTable = New DataTable("PingTable")
Dim Column1 As DataColumn = New DataColumn("Computer Name")
Column1.DataType = System.Type.GetType("System.String")
Dim Column2 As DataColumn = New DataColumn("Status")
Column2.DataType = System.Type.GetType("System.String")
Dim Column3 As DataColumn = New DataColumn("IP Address")
Column3.DataType = System.Type.GetType("System.String")
Dim Column4 As DataColumn = New DataColumn("Bytes Sent")
Column4.DataType = System.Type.GetType("System.String")
Dim Column5 As DataColumn = New DataColumn("Round Trip")
Column5.DataType = System.Type.GetType("System.String")
Dim Column6 As DataColumn = New DataColumn("TTL")
Column6.DataType = System.Type.GetType("System.String")
oDataTable.Columns.Add(Column1)
oDataTable.Columns.Add(Column2)
oDataTable.Columns.Add(Column3)
oDataTable.Columns.Add(Column4)
oDataTable.Columns.Add(Column5)
oDataTable.Columns.Add(Column6)
If fileExists Then
Using sReader As New StreamReader(resultFilePath)
While sReader.EndOfStream = False
Dim rowAdd As DataRow
rowAdd = oDataTable.NewRow()
rowvalue = sReader.ReadLine()
cellvalue = rowvalue.Split(","c)
rowAdd.ItemArray = cellvalue
oDataTable.Rows.Add(rowAdd)
End While
sReader.Close()
End Using
dataPrint(dataName, oDataTable)
i = i + 1
Else
MsgBox("Error! The file for Room" & fName(i) & "is not found.", vbOKOnly = MsgBoxStyle.Critical, "Error")
Exit Do
End If
Loop
End Sub
Private Sub dataPrint(iName As String, iTableData As DataTable)
Dim dgvName As String = iName
Dim oDataTable As DataTable = iTableData
If dgvName = "dgvA1402" Then
dgvA1402.DataSource = oDataTable
ElseIf dgvName = "dgvA1532" Then
dgvA1532.DataSource = oDataTable
ElseIf dgvName = "dgvA1D038" Then
dgvA1D038.DataSource = oDataTable
ElseIf dgvName = "dgvA1D042" Then
dgvA1D042.DataSource = oDataTable
ElseIf dgvName = "dgvA1D043B" Then
dgvA1D043B.DataSource = oDataTable
ElseIf dgvName = "dgvA1E014A" Then
dgvA1E014A.DataSource = oDataTable
ElseIf dgvName = "dgvA1E036" Then
dgvA1E036.DataSource = oDataTable
ElseIf dgvName = "dgvA1E047C" Then
dgvA1E047C.DataSource = oDataTable
ElseIf dgvName = "dgvA1E047D" Then
dgvA1E047D.DataSource = oDataTable
ElseIf dgvName = "dgvA1E047F" Then
dgvA1E047F.DataSource = oDataTable
ElseIf dgvName = "dgvA1E047G" Then
dgvA1E047G.DataSource = oDataTable
ElseIf dgvName = "dgvA1E048" Then
dgvA1E048.DataSource = oDataTable
End If
End Sub
答案 0 :(得分:0)
首先,我要感谢Marc Lyon回应。但是,我认为我没有按照自己的方式提出我找到的答案。
但经过一些研究,然后经过大量的反复试验,以下代码就是我提出的完全可行的代码,并为我提供了我想要的输出。可能有一个更短的方法来实现答案,但现在这是有效的。
Private Sub loadResults(aRooms As List(Of String), pingUp As Integer, pingDown As Integer)
'//All passed variables
Dim rmNames As List(Of String) = aRooms
'//All file path and directory variables
Dim AppDir As String = Path.GetDirectoryName(Path.GetDirectoryName(Application.StartupPath))
Dim resultFilePath As String
Dim fileExists As Boolean
'//All row and row data variables
Dim rowvalue As String
Dim cellvalue(5) As String
'//All datatable and dataset variables
Dim dSet As DataSet
Dim oDataTable As DataTable
'//Displays the total number of ping Successes and Fails
lblGoodPingTotal.Text = pingUp
lblBadPingTotal.Text = pingDown
lblTimeElapsed.Text = GlobalVariables.elapsedTime
'//Sets the new dataset
dSet = New DataSet()
For Each fileName As String In rmNames
oDataTable = New DataTable(fileName)
'//Creating the columns and column names in code
Dim Column1 As DataColumn = New DataColumn("Computer Name")
Column1.DataType = System.Type.GetType("System.String")
Dim Column2 As DataColumn = New DataColumn("Status")
Column2.DataType = System.Type.GetType("System.String")
Dim Column3 As DataColumn = New DataColumn("IP Address")
Column3.DataType = System.Type.GetType("System.String")
Dim Column4 As DataColumn = New DataColumn("Bytes Sent")
Column4.DataType = System.Type.GetType("System.String")
Dim Column5 As DataColumn = New DataColumn("Round Trip")
Column5.DataType = System.Type.GetType("System.String")
Dim Column6 As DataColumn = New DataColumn("TTL")
Column6.DataType = System.Type.GetType("System.String")
'//Setting the columns for the generated datatable
oDataTable.Columns.Add(Column1)
oDataTable.Columns.Add(Column2)
oDataTable.Columns.Add(Column3)
oDataTable.Columns.Add(Column4)
oDataTable.Columns.Add(Column5)
oDataTable.Columns.Add(Column6)
'//Setting the output file path
resultFilePath = AppDir & "\data\output\" & fileName & ".txt"
'//Setting the path to test for file existence
fileExists = File.Exists(resultFilePath)
If fileExists Then
'//If the file exists the file is read into memory and the datatable has it's values assigned to a row
Using sReader As New StreamReader(resultFilePath)
While sReader.EndOfStream = False
'//New row created
Dim rowAdd As DataRow
rowAdd = oDataTable.NewRow()
'//New row has the data read and assigned to it
rowvalue = sReader.ReadLine()
cellvalue = rowvalue.Split(","c)
rowAdd.ItemArray = cellvalue
'//Datatabl gets the row of data assigned to it, one at a time
oDataTable.Rows.Add(rowAdd)
End While
sReader.Close()
End Using
'//Commits the changes to each datatable that's created
oDataTable.AcceptChanges()
'//Adds each datatable that's created to the Dataset for use in output
dSet.Tables.Add(oDataTable)
Else
'//Error message just incase a output file wasn't generated for the selected room
MsgBox("Error! The file for Room " & fileName & " is not found.", vbOKOnly = MsgBoxStyle.Critical, "Error")
End If
Next
'//Calling the sub-routine to output the date to the proper datagridview
dataPrint(dSet)
End Sub
Private Sub dataPrint(iDataSet As DataSet)
'//Variables cast for getting the Dataset and DataTable collections
Dim datSet As DataSet = iDataSet
Dim datTCollection As DataTableCollection = datSet.Tables
'//Running the checks against the selected Dataset for Datatable names and information output
For Each tbName As DataTable In datTCollection
If tbName.TableName = "A1402" Then
dgvA1402.DataSource = tbName
ElseIf tbName.TableName = "A1532" Then
dgvA1532.DataSource = tbName
ElseIf tbName.TableName = "A1D038" Then
dgvA1D038.DataSource = tbName
ElseIf tbName.TableName = "A1D042" Then
dgvA1D042.DataSource = tbName
ElseIf tbName.TableName = "A1D043B" Then
dgvA1D043B.DataSource = tbName
ElseIf tbName.TableName = "A1E014A" Then
dgvA1E014A.DataSource = tbName
ElseIf tbName.TableName = "A1E036" Then
dgvA1E036.DataSource = tbName
ElseIf tbName.TableName = "A1E047C" Then
dgvA1E047C.DataSource = tbName
ElseIf tbName.TableName = "A1E047D" Then
dgvA1E047D.DataSource = tbName
ElseIf tbName.TableName = "A1E047F" Then
dgvA1E047F.DataSource = tbName
ElseIf tbName.TableName = "A1E047G" Then
dgvA1E047G.DataSource = tbName
ElseIf tbName.TableName = "A1E048" Then
dgvA1E048.DataSource = tbName
End If
Next
End Sub
我经历了并重新做了很多代码。我采用了我试图做的核心思想,然后对它们进行了改进(不仅仅是在这些代码块中)。这有点帮助,因为它比我原来的代码段更好地传输数据。
无论如何,在主要答案中,我在DataTetss上找到了有关DataSet的信息,并使用DataSet来存储具有不同名称的多个DataTable。名称由读入的输出文件本身提供。所以我之后需要做的就是将DataSet作为Collection进行搜索,并将名称与TabPages的名称进行匹配,然后将相应的表数据作为DataSource输出到相应的DataGridView。