Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 69
  • Last Modified:

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

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
mainrotor
Asked:
mainrotor
1 Solution
 
ArkCommented:
    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
 
mainrotorAuthor Commented:
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Tackle projects and never again get stuck behind a technical roadblock.
Join Now