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.

    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, 7 months ago Modified 7 years, 11 months ago Viewed 320k 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 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 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 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 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 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 Follow answered Oct 3, 2012 at 2:31 cjbarth's user avatar cjbarthcjbarth 4,3816 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 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 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
  • The return of Staging Ground to Stack Overflow
  • Policy: Generative AI (e.g., ChatGPT) is banned
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

  • Issue in HypergeometricPFQ function:
  • Keep your ----- ◯ and find the answer!
  • Can I race if I cannot or no longer ride a sit up cycle?
  • How could international companies guarantee the loyalty of offshore subsidiaries?
  • Is it plausible for the UK opposition to be led by a lord?
  • Can 3-speed 24/26" adult trike "keep up" with standard mountain/gravel bikes?
  • Proof that independence implies monotonicity in Osborne and Rubinstein
  • Is it possible for Mathematica to output the name of a matrix as opposed to its matrix form?
  • Upgrading PHP versions from buster to bullseye, when I am already on bullseye
  • Can the Orient Express be motorized to run on standard track?
  • Defining a functional map
  • What aspects define how present the garlic taste in an aglio e olio pasta becomes?
  • Should I practise a piece at a metronome tempo that is faster than required?
  • What happens if you don't appear for jury duty for legitimate reasons in the state of California?
  • NaN is not equal to NaN
  • "Stop __________ and hurry up!"
  • Why is "Colourless green ideas sleep furiously" considered meaningless?
  • 50 ohm vs HiZ setting on function generator?
  • Can I use plywood for an exterior dog house?
  • Were there 3D accelerators built on discrete logic?
  • How can UserA programmatically replace a text string within a file they own that's located in /etc?
  • Can RF-S lenses mount on Canon EOS C70?
  • How do you tell if you're in a nebula?
  • Why do some GA aircraft need weights on the control surfaces?
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