私は自分の問題に対する答えを探していましたが、残念ながら成功していません。
まず、私のコードは次のとおりです。
For x = 0 To (NumberOfRows - 1)
For y = 0 To (NumberOfColumns - 1)
DataGridView1.Rows(x).Cells(y).Value = TrimmedData(ArrayIndex)
ArrayIndex = ArrayIndex + 1
Next
Next
datagridviewの列数と行数を事前に設定しました。次に、上記のコードは、データグリッドビューに文字列配列からのデータを入力するために、2つのforループを循環します。
問題はここにあるこの行です:
DataGridView1.Rows(x).Cells(y).Value = TrimmedData(ArrayIndex)
変数xとy(行とセルの位置を決定するため)がある場合は、変数xまたは変数y(もう一方は固定数です。たとえばこのコード)がある場合は問題なく機能します。結構です:
DataGridView1.Rows(x).Cells(0).Value = TrimmedData(ArrayIndex)
このようなループでデータを入力する方法を教えてもらえますか?
前もって感謝します。
編集、ここに完全なサブルーチンコードがあります:
Private Sub ExtractData()
Dim x As Decimal = 0
Dim y As Decimal = 0
Dim GetData As String = " StartOfData ,10,03,John Smith,8207176,a,c,d,b,d,a,b,d,c,b,Bill McBill,8109871,b,d,c,b,a,d,c,b,d,a,Amy Bunton,8212345,a,d,c,a,d,b,c,d,b,c, EndOfData "
Dim TrimmedData() As String = GetData.Split(",")
' we need to find where the start of our data is (we do this because we may have recieved the same data, multiple times.)
While TrimmedData(x) <> " StartOfData "
x = x + 1
End While
Dim StartOfArrayData As Decimal = (x + 1) 'we have just found where StartOfData is written, so we add one to go to the actual start of our data
Dim ArrayIndex As Decimal = StartOfArrayData
Dim NumberOfColumns As Decimal = TrimmedData(ArrayIndex) + 2 'the first number in the array tells us how many questions there are in the exam. We add 2 because we need the student name and ID number
ArrayIndex = ArrayIndex + 1 ' now we are at the location where it tells us how many students there are.
Dim NumberOfRows As Decimal = TrimmedData(ArrayIndex) + 1 ' we need to add one extra row because we have a certain number of rows for our student names + one extra for the headings
ArrayIndex = ArrayIndex + 1
DataGridView1.ColumnCount = NumberOfColumns
DataGridView1.RowCount = NumberOfRows
' and now we need to know where the end of our data is
While TrimmedData(x) <> " EndOfData "
x = x + 1
End While
Dim EndOfArrayData As Decimal = (x - 1) 'we have just found where EndOfData is written, so we minus one to go to the actual end of our data
For x = 0 To (NumberOfRows - 1) ' we minus one because we are starting from 0 and not 1
For y = 0 To (NumberOfColumns - 1) ' we minus one because we are starting from 0 and not 1
DataGridView1.Rows(x).Cells(y).Value = TrimmedData(ArrayIndex)
ArrayIndex = ArrayIndex + 1
Next
Next
End Sub
背景情報について-私は学生がABCDとSUBMITとマークされた5つのボタンが付いた小さなワイヤレスデバイスを持っている試験システムを作ることから始めています。学生はデータプロジェクターに質問が表示され、次にABCDボタンの1つを押しますその後、送信します。この回答は、コンピューターに接続されたマイクロコントローラーベースのデバイスに送信されます。すべての質問に対するすべての回答が送信されると、データがコンピュータープログラム(つまり、私が取り組んでいるこのビジュアルベーシックプログラム)にシリアルに送信されます。このデータは、私のコードで使用されるものになります。一部のデフォルトデータIE
Dim GetData As String = " StartOfData ,10,03,John Smith,8207176,a,c,d,b,d,a,b,d,c,b,Bill McBill,8109871,b,d,c,b,a,d,c,b,d,a,Amy Bunton,8212345,a,d,c,a,d,b,c,d,b,c, EndOfData "
お役に立てば幸いです:)