当使用循环读取值时,vb net超出范围datagriview [英] vb net out of range datagriview when read values with loop

查看:91
本文介绍了当使用循环读取值时,vb net超出范围datagriview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,尝试捕获它工作的dbgrid单元格的第一个字符



I have this code, try to catch the First char of a dbgrid cell it Works

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
               Dim s As String = vbEmpty
       Dim firstChar As Char = ChrW(vbEmpty)
       s = DataGridView1.Rows(1).Cells(2).Value.ToString
       firstChar = s.Chars(0) 'here exception
       TextBox1.Text = firstChar
       If firstChar = "1" Then

           DataGridView2.Rows.Add(1)
           DataGridView2.Rows(1).Cells(0).Value = DataGridView1.Rows(1).Cells(1).Value
           DataGridView2.Rows(1).Cells(1).Value = DataGridView1.Rows(1).Cells(2).Value
       End If

   End Sub





但是当我遍历行时​​会引发异常超出范围





but when I loop through the rows raises exception out of range

Dim s As String
      For i As Integer = 0 To DataGridView1.RowCount - 1

          Dim firstChar As New Char
          s = DataGridView1.Rows(i).Cells(2).Value.ToString
                     firstChar = s.Chars(0) 'exception here
                     If firstChar = "6" Then

              DataGridView2.Rows.Add()
              DataGridView2.Rows(i).Cells(0).Value = DataGridView1.Rows(i).Cells(1).Value
              DataGridView2.Rows(i).Cells(1).Value = DataGridView1.Rows(i).Cells(2).Value
          End If
      Next

推荐答案

我不确定 for 循环是否包含页眉和页脚行,但是是的,然后下面的解决方如果不是,请忽略此解决方案。



从index = 1开始循环,如

I am not sure about whether for loop includes the header and footer rows or not but if yes then following solution may help. If no, please ignore this solution.

Either start your loop from index=1 like
Dim s As String
        For i As Integer = 1 To DataGridView1.RowCount - 1 'RowCount-2 if footer is enabled
 
            Dim firstChar As New Char
            s = DataGridView1.Rows(i).Cells(2).Value.ToString
                       firstChar = s.Chars(0) 'exception here
                       If firstChar = "6" Then
 
                DataGridView2.Rows.Add()
                DataGridView2.Rows(i).Cells(0).Value = DataGridView1.Rows(i).Cells(1).Value
                DataGridView2.Rows(i).Cells(1).Value = DataGridView1.Rows(i).Cells(2).Value
            End If
        Next





或者,排除标题&来自循环的页脚行如



OR, exclude header & footer rows from loop like

Dim s As String
        For i As Integer = 0 To DataGridView1.RowCount - 1
           If row.RowType = DataControlRowType.DataRow Then
            Dim firstChar As New Char
            s = DataGridView1.Rows(i).Cells(2).Value.ToString
                       firstChar = s.Chars(0) 'exception here
                       If firstChar = "6" Then
 
                DataGridView2.Rows.Add()
                DataGridView2.Rows(i).Cells(0).Value = DataGridView1.Rows(i).Cells(1).Value
                DataGridView2.Rows(i).Cells(1).Value = DataGridView1.Rows(i).Cells(2).Value
             End If
            End If
        Next


正是为什么我们不能说 - 我们无法检查,因为我们无法访问填充DataGridView的数据。



所以使用d ebugger。运行您的应用程序,当异常发生时,查看触发问题的行的数据:Rows索引值是否存在?有多少列?进入s的字符串有多长?我猜它是空的 - 但没有你的数据,我可以告诉你。



当你知道这一点时,你可以查看导致问题的实际行并找出字符串为空的原因。


但很抱歉,我们不能为你做到这一点!
Exactly why we cannot say - we can't check because we don't have access to your data that fills the DataGridView.

So use the debugger. Run your app and when the exception happens look at the data for the line that triggers the problem: Does the Rows index value exist? How many columns are there? How long is the string that goes into "s"? I'm guessing that it's empty - but without your data I can;t tell.

When you know that, you can look at the actual row that causes the problem and work out why the string is empty.

But sorry, we can't do that for you!


这篇关于当使用循环读取值时,vb net超出范围datagriview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆