在向DGV列添加值时跳过某些单元格

时间:2014-08-29 07:48:22

标签: vb.net loops datagridview

我无法让这个工作。我正在尝试使用来自txt文件的值填充第2列,并跳过第1列中没有值但没有从文本文件中跳过数据的单元格。

这就是我得到的:

enter image description here

我正在使用的代码:

Dim fileName = "X:\2013\NKT13\FI-ZL\BU.rev"
        Dim lineCount = System.IO.File.ReadAllLines(fileName).Length
        Dim lines() = System.IO.File.ReadAllLines(fileName)


        For i As Integer = 0 To lineCount
            Dim RM001 As String = lines(i).Replace(".", "")
            Dim LBS001() As String = RM001.Split(New String() {";"}, StringSplitOptions.None)
            On Error Resume Next
            Dim Val = LBS001(1)
            Dim Val2 = LBS001(2)
            If DataGridView1.Rows(i).Cells(1).Value Is Nothing Then
                'MsgBox(DataGridView1.Rows(i).Index)
                DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.WhiteSmoke
            Else
                For Each row As DataGridViewRow In DataGridView1.Rows
                    DataGridView1.Rows(i).Cells(2).Value = LBS001(0)
                Next
            End If
Next

在上面的代码中If DataGridView1.Rows(i).Cells(1).Value Is Nothing行无效。当该行替换为If DataGridView1.Rows(i).Cells(1).Value = ""时,我得到了这个:

enter image description here

此处缺少A201A,并且对于column1中的每个空行,将跳过1个结果。 如何使用txt文件中的数据填充第2列,以便只跳过行而不是结果。

编辑: 我尝试了其他的东西,它适用于第2列中的第一个空单元格,但是当它涉及到第二个时......它从数组中跳过一个数据然后继续正常直到到达另一个空单元格。示例:A201A ... A224A(一切正常)...空白单元格(跳过)... A205正常但在206单元格中它放置了A207A。

现在,由于史蒂夫,下面的代码也有效。

Dim Dat2() As String = Split(start1(0), Environment.NewLine)
        Dim Dat2A() As String = Split(Dat2(0), ";")
        Dim Dat2B() As String = Split(Dat2(1), ";")


        Dim fileName = "X:\2013\NKT13\FI-ZL\BU.rev"
        Dim lineCount = System.IO.File.ReadAllLines(fileName).Length
        Dim lines() = System.IO.File.ReadAllLines(fileName)

    Dim a As Integer = 0
        For x As Integer = 0 To DataGridView1.Rows.Count - 2

             If DataGridView1.Rows(x).Cells(1).Value <> "" Then
            DataGridView1.Rows(x).Cells(2).Value = Dat2(a)

            a += 1
        Else
            DataGridView1.Rows(x).Cells(2).Value = ""

        End If
            End If
        Next

编辑2: 感谢史蒂夫...我已经改变了一些代码并且它有效。唯一的问题是我在lineCount中缺少两行我认为是因为每个DGV行被跳过。

        Dim fileName = "X:\2013\NKT13\FI-ZL\BU.rev"
    Dim lineCount = System.IO.File.ReadAllLines(fileName).Length
    Dim lines() = System.IO.File.ReadAllLines(fileName)
    MsgBox(lineCount)
    Dim i2 As Integer = 0
    For i As Integer = 0 To lineCount
        Dim RM001 As String = lines(i2).Replace(".", "")
        Dim LBS001() As String = RM001.Split(New String() {";"}, StringSplitOptions.None)
        On Error Resume Next
        Dim Val = LBS001(1)
        Dim Val2 = LBS001(2)
        If DataGridView1.Rows(i).Cells(1).Value <> "" Then
            For Each row As DataGridViewRow In DataGridView1.Rows
                DataGridView1.Rows(i).Cells(2).Value = LBS001(0)

            Next
            i2 += 1
        Else
            DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.WhiteSmoke
        End If
    Next

1 个答案:

答案 0 :(得分:1)

您现在正在做的是使用i来读取行,并i写入您的网格。你需要的是另一个变量,如i2,你可以在网格中设置值时手动递增,或者需要跳过网格中的一行。

Dim i2 as Int32 = 0
For i As Integer = 0 To lineCount
  Dim RM001 As String = lines(i).Replace(".", "")
  Dim LBS001() As String = RM001.Split(New String() {";"}, StringSplitOptions.None)
  On Error Resume Next
  Dim Val = LBS001(1)
  Dim Val2 = LBS001(2)
  If DataGridView1.Rows(i2).Cells(1).Value Is Nothing Then
     'MsgBox(DataGridView1.Rows(i2).Index)
     DataGridView1.Rows(i2).DefaultCellStyle.BackColor = Color.WhiteSmoke
  Else
     For Each row As DataGridViewRow In DataGridView1.Rows
        DataGridView1.Rows(i2).Cells(2).Value = LBS001(0)
     Next
     i2 += 1
  End If
Next

编辑:经过OP的进一步审核和编辑后,这是一个更好的答案:

Dim fileName = "X:\2013\NKT13\FI-ZL\BU.rev"
Dim lines() = System.IO.File.ReadAllLines(fileName)
Dim lineCount = lines.Length
Dim a As Integer = 0
For x As Integer = 0 To DataGridView1.Rows.Count - 1
    If DataGridView1.Rows(x).Cells(1).Value <> "" Then
        DataGridView1.Rows(x).Cells(2).Value = lines(a)
        a += 1
    Else
        DataGridView1.Rows(x).Cells(2).Value = ""        
    End If
Next