how do i add a record to a datatable and then update the datagridview?

delirious

New member
Joined
Nov 24, 2006
Messages
1
Programming Experience
Beginner
VB.NET:
[COLOR=#00007f]Imports[/COLOR] System.Data.OleDb
[COLOR=#00007f]Public[/COLOR] [COLOR=#00007f]Class[/COLOR] AddCustomerForm
    [COLOR=#00007f]Inherits[/COLOR] System.Windows.Forms.Form
    [COLOR=#00007f]Dim[/COLOR] cn [COLOR=#00007f]As[/COLOR] OleDbConnection
    [COLOR=#00007f]Dim[/COLOR] cmd [COLOR=#00007f]As[/COLOR] OleDbCommand
    [COLOR=#00007f]Dim[/COLOR] dr [COLOR=#00007f]As[/COLOR] OleDbDataReader
    [COLOR=#00007f]Dim[/COLOR] icount [COLOR=#00007f]As[/COLOR] [COLOR=#00007f]Integer[/COLOR]
    [COLOR=#00007f]Dim[/COLOR] str [COLOR=#00007f]As[/COLOR] [COLOR=#00007f]String[/COLOR]

    [COLOR=#00007f]Private[/COLOR] [COLOR=#00007f]Sub[/COLOR] AddOKBtn_Click([COLOR=#00007f]ByVal[/COLOR] sender [COLOR=#00007f]As[/COLOR] System.[COLOR=#00007f]Object[/COLOR], [COLOR=#00007f]ByVal[/COLOR] e [COLOR=#00007f]As[/COLOR] System.EventArgs) [COLOR=#00007f]Handles[/COLOR] AddOKBtn.Click
        [COLOR=#00007f]Try[/COLOR]
            cn = [COLOR=#00007f]New[/COLOR] OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=AccessDB.mdb;")
            cn.[COLOR=#00007f]Open[/COLOR]()
            str = "INSERT INTO Customer Values(" & [COLOR=#00007f]CInt[/COLOR](txtID.Text) & ",'" & txtFirstName.Text & "','" & txtLastName.Text & "','" & txtDetails.Text & "','" & txtReceipt.Text & "[COLOR=#007f00]');"
[/COLOR]            cmd = [COLOR=#00007f]New[/COLOR] OleDbCommand(str, cn)
        [COLOR=#00007f]Catch[/COLOR]
        [COLOR=#00007f]End[/COLOR] [COLOR=#00007f]Try[/COLOR]
        cn.[COLOR=#00007f]Close[/COLOR]()
        [COLOR=#00007f]Me[/COLOR].Hide()
    [COLOR=#00007f]End[/COLOR] [COLOR=#00007f]Sub[/COLOR]
I've done this but I've found out that this code inserts the record to the database that's why it's not updated in the datagridview so how do i go about inserting to datatable and then update the datagridview

ps. my datagridview is on a different form from the input controls

mainform > add button > add input controls

I've tried another code

VB.NET:
Public Class AddCustomerForm
 

    Private Sub AddOKBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddOKBtn.Click
        Dim newCustomerRow As AccessDBDataSet.CustomerRow
        newCustomerRow = AccessDBDataSet.Customer.NewCustomerRow()
        newCustomerRow.ID = txtID.Text
        newCustomerRow.FirstName = txtFirstName.Text
        newCustomerRow.LastName = txtLastName.Text
        newCustomerRow.Details = txtDetails.Text
        newCustomerRow.Receipt = txtReceipt.Text
        AccessDBDataSet.Customer.Rows.Add(newCustomerRow)
        AccessDBDataSet.Customer.AcceptChanges()
        MainForm.CustomerTableAdapter.Fill(MainForm.AccessDBDataSet.Customer)
        Me.Hide()
    End Sub

but this only fills the add form with the new data
 
Read the DW2 link in my signature.. the tutorial about fetching data into your app, will tell you to drag a datagrid to your form.. it is bound automatically to a bindingsource and datatable. the BS is linked to a bindingnavigaotr which can add rows. Upon adding a row via the button on the BN, the BS updates its current item and the DGV immediately jumps to the new row..

If you require an example project demonstrating this, i can knock one up in about 30 seconds, without touching the keyboard. Reading the DW2 link will tell you how i did it
 
Back
Top