I'm doing a system in VB.net 2012 where a user inputs a data on a textbox and then if the user clicks on the button, the data in the textbox will show on the datagridview. The problem I'm having is when I input another data, the current data shown in the datagridview is replaced instead of having a new row for the inputted data. This is the code I've worked on:

Private Sub btn_addborrow_Click(sender As Object, e As EventArgs) Handles btn_addborrow.Click
        Dim dt As New DataTable
        Dim dsNewRow As DataRow = dt.NewRow()

        With dgv_booklist
            dt.Columns.Add("ISBN", GetType(String))
            dt.Columns.Add("BookCode", GetType(String))
            dt.Columns.Add("Title", GetType(String))
            dt.Columns.Add("Author", GetType(String))
            dt.Columns.Add("Category", GetType(String))
            dt.Columns.Add("StudentNo", GetType(String))
            dt.Columns.Add("FirstName", GetType(String))
            dt.Columns.Add("LastName", GetType(String))
            dt.Columns.Add("BorrowDate", GetType(String))
            dt.Columns.Add("LoanPeriod", GetType(String))

            dsNewRow.Item("ISBN") = txtbox_isbn.Text
            dsNewRow.Item("BookCode") = txtbox_code.Text
            dsNewRow.Item("Title") = txtbox_title.Text
            dsNewRow.Item("Category") = txtbox_category.Text
            dsNewRow.Item("StudentNo") = txtbox_studno.Text
            dsNewRow.Item("FirstName") = txtbox_firstname.Text
            dsNewRow.Item("LastName") = txtbox_lastname.Text
            dsNewRow.Item("BorrowDate") = dtp_borrowdate.Text
            dsNewRow.Item("LoanPeriod") = txtbox_loanperiod.Text

            dgv_booklist.DataSource = dt

            dt.Rows.Add(New Object() {txtbox_isbn.Text, txtbox_code.Text, txtbox_title.Text, txtbox_author.Text, txtbox_category.Text, txtbox_studno.Text, txtbox_firstname.Text, txtbox_lastname.Text, dtp_borrowdate.Text, txtbox_loanperiod.Text})


        End With

End Sub

I've searched and searched but I really can't add a new row on my datagridview. If you could point out what I did wrong in the code and what I could do to change the mistake, that would be much appreciated.

wow buddy your code doesn't make any sense.
you should run this once

    With dgv_booklist
        .Columns.Add("ISBN", "ISBN")
        .Columns.Add("BookCode", "BookCode")
        .Columns.Add("Title", "Title")
        .Columns.Add("Author", "Author")
        .Columns.Add("Category", "Category")
        .Columns.Add("StudentNo", "StudentNo")
        .Columns.Add("FirstName", "FirstName")
        .Columns.Add("LastName", "LastName")
        .Columns.Add("BorrowDate", "BorrowDate")
        .Columns.Add("LoanPeriod", "LoanPeriod")
    End With

and that's the add row code

Private Sub btn_addborrow_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_addborrow.Click
    dgv_booklist.Rows.Add(txtbox_isbn.Text, txtbox_code.Text, txtbox_title.Text, txtbox_author.Text, txtbox_category.Text, txtbox_studno.Text, txtbox_firstname.Text, txtbox_lastname.Text, dtp_borrowdate.Text, txtbox_loanperiod.Text)
End Sub

I followed your code and it works now. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.