How Do I Add Records To A DataGridView In VB.Net? - Stack Overflow

2024 Developer survey is here and we would like to hear from you! Take the 2024 Developer Survey
    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Explore Teams Create a free Team
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs How do I add records to a DataGridView in VB.Net? Ask Question Asked 15 years, 6 months ago Modified 7 years, 11 months ago Viewed 319k times 17

How do I add new record to DataGridView control in VB.Net?

I don't use dataset or database binding. I have a small form with 3 fields and when the user clicks OK they should be added to the DataGridView control as a new row.

Share Improve this question Follow edited Jul 20, 2016 at 16:07 codeConcussion's user avatar codeConcussion 12.9k8 gold badges49 silver badges63 bronze badges asked Nov 25, 2008 at 14:43 JohnJohn Add a comment |

6 Answers 6

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 36

If you want to add the row to the end of the grid use the Add() method of the Rows collection...

DataGridView1.Rows.Add(New String(){Value1, Value2, Value3})

If you want to insert the row at a partiular position use the Insert() method of the Rows collection (as GWLlosa also said)...

DataGridView1.Rows.Insert(rowPosition, New String(){value1, value2, value3})

I know you mentioned you weren't doing databinding, but if you defined a strongly-typed dataset with a single datatable in your project, you could use that and get some nice strongly typed methods to do this stuff rather than rely on the grid methods...

DataSet1.DataTable.AddRow(1, "John Doe", true) Share Improve this answer Follow answered Nov 25, 2008 at 22:01 codeConcussion's user avatar codeConcussioncodeConcussion 12.9k8 gold badges49 silver badges63 bronze badges Add a comment | 3

I think you should build a dataset/datatable in code and bind the grid to that.

Share Improve this answer Follow answered Nov 25, 2008 at 14:47 Galwegian's user avatar GalwegianGalwegian 41.9k16 gold badges112 silver badges158 bronze badges Add a comment | 2

The function you're looking for is 'Insert'. It takes as its parameters the index you want to insert at, and an array of values to use for the new row values. Typical usage might include:

myDataGridView.Rows.Insert(4,new object[]{value1,value2,value3});

or something to that effect.

Share Improve this answer Follow edited Jun 10, 2012 at 2:45 Siddharth Rout's user avatar Siddharth Rout 148k18 gold badges208 silver badges252 bronze badges answered Nov 25, 2008 at 15:01 GWLlosa's user avatar GWLlosaGWLlosa 24.2k18 gold badges80 silver badges116 bronze badges Add a comment | 1

When I try to cast data source from datagridview that used bindingsource it error accor cannot casting:

----------Solution------------

'I changed casting from bindingsource that bind with datagridview

'Code here

Dim dtdata As New DataTable() dtdata = CType(bndsData.DataSource, DataTable) Share Improve this answer Follow edited Jun 10, 2012 at 2:45 Siddharth Rout's user avatar Siddharth Rout 148k18 gold badges208 silver badges252 bronze badges answered Apr 1, 2011 at 8:45 Mr.Buntha Khin's user avatar Mr.Buntha KhinMr.Buntha Khin 851 silver badge6 bronze badges Add a comment | 1

If you want to use something that is more descriptive than a dumb array without resorting to using a DataSet then the following might prove useful. It still isn't strongly-typed, but at least it is checked by the compiler and will handle being refactored quite well.

Dim previousAllowUserToAddRows = dgvHistoricalInfo.AllowUserToAddRows dgvHistoricalInfo.AllowUserToAddRows = True Dim newTimeRecord As DataGridViewRow = dgvHistoricalInfo.Rows(dgvHistoricalInfo.NewRowIndex).Clone With record newTimeRecord.Cells(dgvcDate.Index).Value = .Date newTimeRecord.Cells(dgvcHours.Index).Value = .Hours newTimeRecord.Cells(dgvcRemarks.Index).Value = .Remarks End With dgvHistoricalInfo.Rows.Add(newTimeRecord) dgvHistoricalInfo.AllowUserToAddRows = previousAllowUserToAddRows

It is worth noting that the user must have AllowUserToAddRows permission or this won't work. That is why I store the existing value, set it to true, do my work, and then reset it to how it was.

Share Improve this answer Follow answered Oct 3, 2012 at 2:31 cjbarth's user avatar cjbarthcjbarth 4,3716 gold badges44 silver badges64 bronze badges Add a comment | 1

If your DataGridView is bound to a DataSet, you can not just add a new row in your DataGridView display. It will now work properly.

Instead you should add the new row in the DataSet with this code:

BindingSource[Name].AddNew()

This code will also automatically add a new row in your DataGridView display.

Share Improve this answer Follow edited Oct 27, 2012 at 0:01 Victor Zakharov's user avatar Victor Zakharov 26.2k18 gold badges90 silver badges152 bronze badges answered Mar 8, 2012 at 2:05 Robert's user avatar RobertRobert 111 bronze badge 1
  • Are you sure this is correct? My BindingSource has no method AddNew(). – acme Commented Apr 19, 2012 at 12:32
Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

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

  • Featured on Meta
  • Upcoming sign-up experiments related to tags
  • Policy: Generative AI (e.g., ChatGPT) is banned
  • The return of Staging Ground to Stack Overflow
  • The 2024 Developer Survey Is Live
Visit chat

Linked

-1 vb.net assigning stored procedure value as a new row to gridview 3 Adding rows to DataGridView in VB.NET 0 How to manually add new rows to a datagrid 0 How can I add rows to a DataGridView control in C#? 0 How to insert row in data grid in VB6 11 How can I manually add data to a dataGridView? 0 Adding row to datagridview 1 Add row to datagridview in vb.net 0 Add items to DataGridView 0 Adding Rows in DataGridView 0 How to add Row to DataGridView in Visual Basic (Visual Studio)

Hot Network Questions

  • What is the goal of the message “astronaut use only” written on one Hubble's module?
  • Irreducible complex representations of some abelian Lie groups
  • What's this plant with saw-toothed leaves, scaly stems and white pom-pom flowers?
  • "The Puzzle benchmark" source?
  • Do abandoned buildings count as broken rooms for the purposes of downtime rules?
  • problem of proving the existence of limit of a integral.
  • Fourth species counterpoint treating fourth
  • Is parapsychology a science?
  • Question about the sum of odd powers equation
  • What is this tool shown to us?
  • What would happen to 'politicians' in this world?
  • Why was the 1540 a computer in its own right?
  • Is it possible to cast a bonus action spell and then use the Ready action to activate a magic item and cast another spell off-turn with your reaction?
  • Is it epistemologically self-consistent to use the scientific method to justify some beliefs and non-scientific justifications for others?
  • Can I remove an appimage file?
  • What distribution should I use to predict three possible outcomes
  • Could a kickback have caused my mitre saw fence to bend?
  • Show or movie where a blue alien is discovered by kids in a storm drain or sewer and cuts off its own foot to escape
  • Containing a Black Hole's End-Of-Life
  • Lilypond scaling a score with many parts to avoid over-full pages
  • How does this over-voltage protection diode work?
  • What was God's original plan for humanity prior to the fall?
  • Confusion with treatment of unit vectors in electrostatics
  • Would a series of gravitational waves from a supernova affect time on a 200 year old clock just as water waves affected clocks on ships in rough seas?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-vb

Từ khóa » Visual Basic Datagridview Insert Row