通过DataGridView自动增加所选行

时间:2018-05-24 14:07:34

标签: vb.net

我正在尝试将选定的行更新为数字,然后将新更新的值之后的每隔一行增加1,使所有先前的行不受影响

我已经选择了要更新的行:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

     Dim i = DataGridView1.CurrentRow.Index

     With DataGridView1
         .Rows(i).Cells("Value").Value = NumTextbox.Text
     End With

我尝试添加:

         .Rows(i).Cells("Value").Value = i + 1

但是只将值标记为行数,是否有人可以指向正确的方向?

2 个答案:

答案 0 :(得分:0)

For j As Integer = i + 1 to DataGridView1.Rows.Count - 1 
DataGridView1.Rows(j).Cells("Value").Value = i + 1
next

答案 1 :(得分:0)

全部放在一起......

Private Sub IncrementRows()
        Dim i = DataGridView1.CurrentRow.Index
        Dim num As Integer
        If Integer.TryParse(NumTextbox.Text, num) Then
            With DataGridView1
                .Rows(i).Cells("Value").Value = num
            End With
            For j As Integer = i + 1 To DataGridView1.Rows.Count - 1
                num += 1
                DataGridView1.Rows(j).Cells("Value").Value = num
            Next
        Else
            MessageBox.Show("Please enter a valid number.")
            NumTextbox.Focus
        End If
End Sub