如何在VB.net的DataGridview中添加最后一个位置的行并从当前位置删除行
我正在使用vb./net。 在网格视图中,每个视图都有两个按钮,如+和-如果用户单击+则需要在最后一个位置添加一行,如果用户单击-则需要从当前位置删除一行 我尝试了以下代码,但它会给我一个错误如何在VB.net的DataGridview中添加最后一个位置的行并从当前位置删除行,vb.net,Vb.net,我正在使用vb./net。 在网格视图中,每个视图都有两个按钮,如+和-如果用户单击+则需要在最后一个位置添加一行,如果用户单击-则需要从当前位置删除一行 我尝试了以下代码,但它会给我一个错误 Private count As Integer = 1 Private Sub dgvSourcePath_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCe
Private count As Integer = 1
Private Sub dgvSourcePath_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvSourcePath.CellContentClick
If e.ColumnIndex = 2 Then
dgvSourcePath.Rows.Insert(count)
End If
If e.ColumnIndex = 3 Then
count -= 1
dgvSourcePath.Rows.RemoveAt(count)
End If
If e.ColumnIndex = 1 Then
OpenSourceFileDialog.ShowDialog()
If Windows.Forms.DialogResult.OK Then
dgvSourcePath.CurrentRow.Cells(0).Value = OpenSourceFileDialog.FileName
End If
End If
dgvSourcePath.Refresh()
End Sub
请试试这个:
Private Sub dgvdatos_CellContentClick(sender As DataGridView, e As DataGridViewCellEventArgs) Handles dgvdatos.CellContentClick
Try
dgvdatos.EndEdit()
If TypeOf (sender.CurrentCell) Is DataGridViewButtonCell Then
If e.ColumnIndex = 2 Then
dgvdatos.Rows.Insert(sender.RowCount - 1)
End If
If e.ColumnIndex = 3 And sender.Rows.Count > 1 Then
count -= 1
dgvdatos.Rows.RemoveAt(e.RowIndex)
End If
If e.ColumnIndex = 1 Then
OpenSourceFileDialog.ShowDialog()
If DialogResult.OK Then
dgvdatos.CurrentRow.Cells(0).Value = OpenSourceFileDialog.FileName
End If
End If
dgvdatos.Refresh()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub请正确设置代码格式。哎呀,如果我们能找到错误消息是什么以及错误发生在哪一行就好了。
CellContentClick
事件为您提供了所单击行的索引。删除行时应该使用该索引。至于添加,当Add
专门添加到集合末尾时,为什么要使用Insert
?