我一直在寻找我的问题的答案,但遗憾的是没有取得任何成功。
首先,这是我的代码:
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并提交,学生将有一个数据投影机上显示的问题,他们再按之一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 "
希望有所帮助:)
答案 0 :(得分:2)
您已指定;
Dim x As Decimal = 0
Dim y As Decimal = 0
将其更改为;
Dim x As Integer = 0
Dim y As Integer = 0
这将解决缩小转换错误...但是,您可能需要查看数据以获取数据...
答案 1 :(得分:0)
从最简单的事情开始,所有硬编码值。如果可行,请逐步添加动态内容。
我猜测它可能在你的数组中,尝试删除它并只使用像“我的数据”这样的字符串。另外,gridview中有页眉还是页脚?暂时删除它们。对不起,我输入了这个作为答案,但我想我还不能发表意见。