【问题标题】:Difficulties populating a datagridview using an inner and outer for loop使用内部和外部 for 循环填充 datagridview 的困难
【发布时间】: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


【解决方案1】:

你已经指定了;

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

改成;

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

这将解决 Narrowing Conversion 错误...但是,您可能需要查看数学以获取数据...

【讨论】:

  • 是的,做到了!非常感谢那个 PGallagher,我感激不尽:)
  • 完全没有问题!很高兴我能帮上忙!
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多