1

我一直在寻找我的问题的答案,但不幸的是没有任何成功。

首先,这是我的代码:

    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预先设置了列数和行数。然后,上面的代码循环通过两个 for 循环,以尝试使用字符串数组中的数据填充 datagridview。

问题是这里的这一行:

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 的按钮,学生将在数据投影仪上显示问题,然后他们按下 ABCD 按钮之一然后提交。该答案将发送到连接到计算机的基于微控制器的设备。一旦提交了所有问题的所有答案,它就会将数据串行发送到计算机程序(即我正在处理的这个 Visual Basic 程序),这些数据将在我的代码中使用 - 目前我只是通过输入来测试它在一些默认数据 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 "

希望有帮助:)

4

2 回答 2

2

您已指定;

Dim x As Decimal = 0
Dim y As Decimal = 0

将其更改为;

Dim x As Integer = 0
Dim y As Integer = 0

这将解决缩小转换错误...但是,您可能需要查看数学以获取数据...

于 2013-01-25T00:22:56.873 回答
0

从最简单的事情开始,所有硬编码值。如果可行,请逐渐添加动态内容。

我猜它可能在你的数组中,尝试删除它并使用像“我的数据”这样的字符串。另外,您在gridview 中有页眉或页脚吗?现在也删除那些。抱歉,我输入了这个作为答案,但我认为我还不能发表评论。

于 2013-01-25T00:17:28.707 回答