使用数据表 Vb.NET 添加带有按钮的新行到 Gridview

Add New row With button to Gridview using datatable Vb.NET

请帮帮我 我想在使用数据表单击“获取”按钮时添加一个新行,我正在使用这个 sub

在顶部,我使用 Private dr 作为 Datarow=dt.newrow(),Private dt 作为 Datatable,Pos 作为 Integer

Private Sub AddDataTable
    User = txtUser.Text
    Password = txtPass.Text
    Address = txtAddress.Text

    'add Row...
    dr(0) = User
    dr(1) = Password
    dr(2) = Address
    'dt.Rows.Add(dr)
    dt.Rows.InsertAt(dr, Pos)
    Pos = Pos + 1
End Sub

获取事件点击

Private Sub btGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btGet.Click
        dgvUser.DataSource = dt
    End Sub

和最后一个添加事件点击

Private Sub btAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btAdd.Click
        AddDataTable()
        txtUser.Clear()
        txtPass.Clear()
        txtAddress.Clear()
    End Sub

结果总是 1 行,当我再次尝试添加新行时,它给我一个错误

AddDataTable 方法中创建新行并在末尾设置数据源

Private Sub AddDataTable
    Dim dr1 As DataRow = dt.NewRow
    User = txtUser.Text
    Password = txtPass.Text
    Address = txtAddress.Text

    'add Row...
    dr1 (0) = User
    dr1 (1) = Password
    dr1 (2) = Address
    dgvUser.DataSource = dt;
End Sub