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

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

  • Why no "full-stack" SQL-like language?
  • If I sacrifice a Forsaken Miner to the card Eaten Alive do I get the miner back?
  • 70s or 80s sci-fi book, a small community try to save the world
  • 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?
  • Snowshoe design for satyrs and fauns
  • Regarding Isaiah 9:6, which text has the original rendering, LXX or MT, and why does the false rendering differ significantly from the original?
  • Why does David Copperfield say he is born on a Friday rather than a Saturday?
  • Does the double origin plane have a cut point?
  • British TV show about a widowed football journalist
  • 70s or 80s sci-fi book, boy has secateur hand
  • Is SQL Injection possible if we're using only the IN keyword (no equals = operator) and we handle the single quote
  • How many rings does cubane have?
  • Revise & Resubmit: changing the text color of revisions in the text?
  • What type of threshold should I buy to replace the one that goes to my garage?
  • Print the largest hidden double
  • Place 5 dominoes so that horizontal and vertical sums are equal
  • How do I get rid of the yellow tint when using Sky Texture?
  • How to determine what is opening tmp files when I invoke a subshell with ksh
  • What would it take for an AI to have beliefs?
  • Bracket matching - Advent of Code 2021 Day 10
  • Expressing an assumption that all variables are mutually distinct and come from a specified set
  • Notepad++ find and replace string
  • How does the TeX primitive `\radical"270370` work?
  • have someone to do something
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 Add Row