0

私はこのコードを持っています

'Open a file for reading
'Get a StreamReader class that can be used to read the file
Dim objStreamReader As StreamReader
Dim variableArray(20) As String
objStreamReader = File.OpenText(filePath)

'Read one line at a time
Dim someString As String
Dim variableNum As Integer = 0
'Iterate through lines
While objStreamReader.Peek() <> -1
    someString = objStreamReader.ReadLine()
    variableArray(variableNum) = someString
    variableNum = variableNum + 1
End While
For Each line As String In variableArray

Next
objStreamReader.Close()

結果をログファイルに出力し、各行に追加され、「|」で区切られたvbscriptがあります。2つの列のみがあります。

これがVBScriptコードのスニペットです

f1.WriteLine("Server Name " & "|" & strName)
f1.WriteLine("OS Name: " & "|" & strCaption)
f1.WriteLine("OS Version: " & "|" & strVersion
f1.WriteLine("CSD Version: " & "|" & strCSDVer
f1.WriteLine("Serial Number: " & "|" & strSerial

コードのForEach部分にこれを読み取らせ、分解して、結果を示すテーブルを作成するにはどうすればよいですか。

4

2 に答える 2

1

テーブルに新しい行を追加するためにvariableArrayから2つの値が必要であることを考慮して、For ...Eachの代わりにFor..Nextループ(2ずつ)を実行します。

Dim myTable As New Table
Dim loopCount As Integer

For loopCount = 0 To variableNum Step 2

    Dim myRow As New TableRow
    Dim myCell1 As New TableCell
    Dim myCell2 As New TableCell

    myCell1.Text = variableArray(loopCount)
    myCell2.Text = variableArray(loopCount + 1)
    myRow.Cells.Add(myCell1)
    myRow.Cells.Add(myCell2)
    myTable.Rows.Add(myRow)

Next

配列内の要素の量はすでに「variableNum」に格納されているため、0からその値まで2ずつループすることができます。反復ごとに、現在の変数と次の変数の値を使用して2つのセルを作成します。配列。次に、それらのセルが行に追加され、次にテーブルに追加されます。

于 2012-08-07T16:34:49.493 に答える
1

DataTableを宣言する

Dim table As DataTable = new DataTable("MyTable")

Foreachの内部:

Dim LineArray() As String = Split(line, "|") 'This will break apart each line into its two parts
'Now add each item of LineArray to the datatable. AKA
Dim column As DataColumn = new DataColumn()
column.DataType = System.Type.GetType("System.Int32")
column.ColumnName = LineArray(0)
column.ReadOnly = True
column.Unique = True
table.Columns.Add(column)

Dim row As DataRow 
row = table.NewRow()
row(LineArray(0)) = LineArray(1)
table.Rows.Add(row)

あなたが実装しようとしているロジック全体はわかりませんが、それはあなたに良いスタートを与えるはずです。基本的には、最初の列を取得してテーブル内の列として設定し、次に2番目の列を取得してその行の値を作成します。

DataTableを作成したら、HTMLの意味でテーブルであるGridViewにバインドできます。

お役に立てれば。

于 2012-08-07T16:38:32.047 に答える