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 vb.net code to insert data on datagridview to access database 3
  • Member Avatar Vb.net Projects 11
  • Member Avatar Checkbox in the Title Bar 2
  • Member Avatar VB.NET ERROR "parameter is not valid" 5
  • Member Avatar A 21320115 DUT 2
  • Member Avatar Vb.net 4
  • Member Avatar accessing database 1
  • Member Avatar vb.net help for date 6
  • Member Avatar link 'drag/drop GUI' to 'C++ Code' 15
  • Member Avatar vb.net invokescript to call ajax function 1
  • Member Avatar Disabling and enabling ctrl keys 14
  • Member Avatar Vb.Net - Database Password Save 5
  • Member Avatar code for login forum in c# 2
  • Member Avatar Vb.net with SQL 3
  • Member Avatar Account Program 0
  • Member Avatar Vb.net Sample Project 16
  • Member Avatar Overloading a function 8
  • Member Avatar VB.NET Tally radio button and save the answer in sql database 8
  • Member Avatar Vb.net and Jemter Testing 4
  • Member Avatar VB.Net with External File 5
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 Add Row