【发布时间】:2013-01-25 08:23:17
【问题描述】:
我一直在寻找问题的答案,但很遗憾没有成功。
首先,这是我的代码:
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
关于背景信息 - 我刚开始制作一个考试系统,学生有一个小型无线设备,上面有五个标记为 A B C D 和 SUBMIT 的按钮,学生将在数据投影仪上显示问题,然后他们按下其中一个按钮A B C D 按钮然后提交。这个答案将被发送到连接到计算机的基于微控制器的设备。一旦提交了所有问题的所有答案,它就会将数据串行发送到计算机程序(即我正在处理的这个 Visual Basic 程序),这些数据将在我的代码中使用 - 目前我只是通过输入来测试它在一些默认数据 I.E.
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 "
希望有帮助:)
【问题讨论】:
-
您收到的错误信息是什么?
-
好点,它显示“重载解析失败,因为在没有缩小转换的情况下无法调用任何可访问的‘项目’。”
-
你的列是什么类型...例如...我已经用三个 DataGridViewTextBoxColumn 测试了上面的内容...尽管我必须在内部循环中添加一个新行才能使其工作;-) 我怀疑您可能在某处使用了另一种列类型?
-
作为一个附带问题....我可以问一下您的“TrimmedData”数组中包含什么。我只是想知道您为什么要以这种方式填充 DataGridView?
-
说实话,我没有考虑过列的类型。他们默认为文本框列吗?例如,如果我有这行代码 DataGridView1.ColumnCount = NumberOfColumns 将默认为 textboxcolumns (因为这就是我创建列数的方式,同样是行数。)
标签: vb.net