Adding New Row In Datagridview [SOLVED] - - Daniweb

Adding new row in datagridview
  1. Home
  2. Programming Forum
  3. Software Development Forum
  4. Discussion / Question
Member Avatar for Mae_1

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.

vb.net 0 0 Share
  • 2 Contributors
  • 2 Replies
  • 15K Views
  • 9 Hours Discussion Span
  • Latest Post 10 Years Ago Latest Post by Mae_1
Member Avatar for oussama_1

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 1 0 Share Member Avatar for Mae_1

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

0 0 Share Share Facebook Twitter LinkedIn Reply to this topic 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.

Sign Up — It's Free! Related Topics
  • Member Avatar Need Help : How to search/filter DataGridView with Textbox 2
  • Member Avatar how to pass textbox to datagrid 4
  • Member Avatar Checkbox in the Title Bar 2
  • Member Avatar Search DataGridView in VB.NET 4
  • Member Avatar Inserting videos and sound clips 2
  • Member Avatar DataGridView multiple tables 1
  • Member Avatar Tuple List 7
  • Member Avatar datagridview select multiple rows 4
  • Member Avatar Form Is Scrolling Down 5
  • Member Avatar Editing a datagridview celltype 6
  • Member Avatar new screen for assembly language 2
  • Member Avatar Adding records to Data Grid View from text files. Using lists - error 1
  • Member Avatar Writing a function, calling it and reading it 2
  • Member Avatar Populate TextBox from DataGridView 5
  • Member Avatar Converting a C subroutine to Assembler 2
  • Member Avatar Tranfer of datagridview value to another form 1
  • Member Avatar Overloading a function 8
  • Member Avatar Automatically update new value in DataGridView and create chart 12
  • Member Avatar How to search Data by textbox and date in VB.net 7
  • Member Avatar Trouble Procedurally Changing Textbox Settings 16
Not what you need?

Reach out to all the awesome people in our software development community by starting your own topic. We equally welcome both specific questions as well as open-ended discussions.

Start New Topic Topics Feed Reply to this Topic Edit Preview Message H1 H2 Post Reply

Share Post

Permanent Link Facebook Facebook Twitter Twitter LinkedIn LinkedIn

Insert Code Block

Insert Code Block Search Search
  • Forum Categories
  • Hardware/Software
    • Recommended Topics
  • Programming
    • Recommended Topics
  • Digital Media
    • Recommended Topics
  • Community Center
    • Recommended Topics
  • Latest Content
  • Newest Topics
  • Latest Topics
  • Latest Posts
  • Latest Comments
  • Top Tags
  • Topics Feed
  • Social
  • Forums
  • Top Members
  • Meet People
  • Community Functions
  • DaniWeb Premium
  • Newsletter Archive
  • Markdown Syntax
  • Community Rules
  • Developer APIs
  • Connect API
  • Forum API Docs
  • Tools
  • SEO Backlink Checker
  • Legal
  • Terms of Service
  • Privacy Policy
  • FAQ
  • About Us
  • Advertise
  • Contact Us
© 2024 DaniWeb® LLC

Từ khóa » Visual Basic Datagridview Insert Row