Solved

I need help inserting records from a bound datagridview control in vb.net

Posted on 2016-10-24
3
28 Views
Last Modified: 2016-11-16
Hi Experts,
I have a DataGrieView control in my VB.Net application which I load bind to a tableadapter programmatically.
I am able to update existing records from the datagridview control to the underlying database, but I am not able to insert\add records from the datagridview control to the database.

What am I doing wrong?  How can I fix this?


Below, I have added my code, and also the error message I get when I try to add the record to the datagridview:

Code
Code image

Error Message
Error Message
Is there a way to programmatically add the ProviderMasterID, when I click on the Drop Down combobox?

Thank you very much in advance,
mrotor
0
Comment
Question by:mainrotor
3 Comments
 
LVL 27

Accepted Solution

by:
Ark earned 500 total points
ID: 41858085
    Private Sub TblProviderBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TblProviderBindingSource.AddingNew
        Dim dv As DataView = TryCast(TblProviderBindingSource.List, DataView)
        Dim rv As DataRowView = dv.AddNew()

        rv("LkLanguageId") = 12345
        e.NewObject = rv
        TblProviderBindingSource.MoveLast()
    End Sub

Open in new window

0
 

Author Comment

by:mainrotor
ID: 41859097
Ark,
You were close, but I kept getting the error below:

Error:
Error i was getting when trying to add a row
I fixed it by adding a line of code and modifying your procedure a little (see code below):
Reference to were I got the additional line of code from:

Link to where I got the additional line of code from.

    Private Sub TblProviderBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TblProviderBindingSource.AddingNew
	Dim bindingSource As BindingSource = CType(sender, BindingSource)	' Added Code
        Dim dv As DataView = TryCast(bindingSource.List, DataView)	                'Modified Code
        Dim rv As DataRowView = dv.AddNew()

        rv("LkLanguageId") = 12345
        e.NewObject = rv
        bindingSource.MoveLast()					                                                  'Modified Code
    End Sub

Open in new window



Thank you very  much.  You will still get the full credit.  I really appreciate your help.  I had been struggling with this issue for quite a bit.

Thanks,
mrotor
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now