0

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 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.