Solved

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

Posted on 2016-10-24
3
55 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 28

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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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