0
votes

Please help me I want to add a new row when click Get Button using datatable, I'm using this sub

on the top I'm using Private dr as Datarow=dt.newrow(), Private dt as Datatable, Pos as 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

For Get event click

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

and the last one Add event Click

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

the Result is always 1 rows, and when I try add a new row again it give me an Error

1

1 Answers

0
votes

create new row insode the AddDataTable method and set the datasouce at the end

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