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

    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.

    Try Teams for free Explore Teams
  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 16 years ago Modified 8 years, 5 months ago Viewed 323k 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 badges50 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 badges50 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 42.2k16 gold badges113 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 149k18 gold badges209 silver badges256 bronze badges answered Nov 25, 2008 at 15:01 GWLlosa's user avatar GWLlosaGWLlosa 24.4k18 gold badges82 silver badges118 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 149k18 gold badges209 silver badges256 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,4696 gold badges45 silver badges65 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.4k18 gold badges93 silver badges155 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 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.

  • The Overflow Blog
  • AI agents that help doctors get paid
  • Legal advice from an AI is illegal
  • Featured on Meta
  • The December 2024 Community Asks Sprint has been moved to March 2025 (and...
  • Stack Overflow Jobs is expanding to more countries
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

  • I was given a used road bike, should I be concerned about the age of the frame, and can I replace it and reuse the other parts?
  • Overstay 3 days
  • On the continuity a function given by evaluating compact subsets of smooth functions
  • Obstruction theory for specializing perfect complexes?
  • Quiz interface based on Yaml files
  • Has there been a clarification regarding whether the use of 'Get Real' move is encouraged or discouraged?
  • Why did the Civil Service in the UK become so associated with Oxbridge?
  • What is the meaning of the second line of the piecewise output of SeriesCoefficient[]?
  • Why are there no no-attribution licenses other than "public domain"?
  • In general relativity, how do we know when the coordinates we compute are physical observables?
  • Should I remove extra water that leaked into sauerkraut?
  • 'exec fish' at the very bottom of my '.zshrc' - is it possible to bypass it?
  • Would having a review article published in a reputable journal as sole author look good to grad programs?
  • Looking for a word or a term similar to Auteur, applicable to app makers
  • Which accents *don't* merge FIRE and HIRE? What about RITE and RIDE?
  • Why does the media establishment still refer to the Syrian revolutionary forces as rebels?
  • Why no "full-stack" SQL-like language?
  • Does fringe biology inpsire fringe philosophy?
  • How to teach high school students to analyze diagrams in a proof?
  • Does using multiple batteries in series or parallel affect mAh
  • Whose logo for an invited seminar in another university?
  • 70s or 80s sci-fi book, boy has secateur hand
  • Few doubts about "A new elementary proof of the Prime Number Theorem" by Richter
  • Issue with Blender Spiral Curve
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