scias23 1 Junior Poster in Training

i've written this code, it works. whenever I click the OK button of the NewModel.vb, it will insert a new record, then the datasource of the datagridview will be reinstantiated and rebinded with all the records in the database. the datagridview will be rebinded with the records from the database.

The GetData function reinstantiates the datatable i use and re-fills the bindingsource.

with the help of an If statement, i re-fill the datatable with all the records from the database to reflect the newly inserted record in the datagridview. however, doing this seems tedious as everytime the condition sets to true, the datatable is reinstantiated and the same data from the database are fetched PLUS the newly inserted record.

maybe it would be better to retrieve just the newly inserted record, then add it in the datagridview using the datatable.

what i want to happen is, i want to check the database for newly inserted record, and if it finds one, the new record will be inserted in a datagridview. -- so if i add another record to the database, the newly added record will immediately appear on the datagridview.

how can i do this?

please help.

Model.vb

Imports System.Data.SqlClient

Public Class Model
    Public sqldata As SqlDataAdapter
    Public modeldata As DataTable
    Public datasource As New BindingSource
    Dim sqlquery As String
    Dim conn As New SqlConnection
    Dim Result As DialogResult

    Public Function GetData(ByVal selectCommand As String)
        sqldata = New SqlDataAdapter(selectCommand, conn)
        modeldata = New DataTable
        modeldata.Locale = System.Globalization.CultureInfo.InvariantCulture
        sqldata.Fill(modeldata)
        datasource.DataSource = modeldata
        Return datasource
    End Function

    Public Sub FillgridPlaneModel()
        gridPlaneModel.DataSource = GetData("SELECT Manufacturer, Model, NRows, NCols FROM PlaneTypes ORDER BY Id ASC")
    End Sub

    Public Sub ShowNewModel()
        Dim NewPlaneModel As New NewModel
        Result = NewPlaneModel.ShowDialog()
    End Sub

    Public Sub ShowEditModel()
        Dim EditPlaneModel As New EditModel
        Result = EditPlaneModel.ShowDialog()
    End Sub

    Private Sub Model_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            conn = GetConnect()
            conn.Open()

            FillgridPlaneModel()
            gridPlaneModel.AutoResizeColumn(0)
            gridPlaneModel.AutoResizeColumn(1)
            gridPlaneModel.AutoResizeColumn(2)
            gridPlaneModel.AutoResizeColumn(3)
            gridPlaneModel.AllowUserToResizeColumns = False
            gridPlaneModel.AllowUserToResizeRows = False
            gridPlaneModel.AllowUserToAddRows = False
        Catch ex As Exception
            MsgBox("SQL SERVER ERROR", MsgBoxStyle.Critical)
        Finally
            conn.Close()
        End Try
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        ShowNewModel()
        If Result = Windows.Forms.DialogResult.OK Then
            gridPlaneModel.DataSource = GetData("SELECT Manufacturer, Model, NRows, NCols FROM PlaneTypes ORDER BY Id ASC")
        End If
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        ShowEditModel()
        If Result = Windows.Forms.DialogResult.OK Then
            '
        End If
    End Sub

    Private Sub gridPlaneModel_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridPlaneModel.CellContentClick

    End Sub
End Class

NewModel.vb

Imports System.Data.SqlClient

Public Class NewModel
    Dim sqldata As SqlDataAdapter
    Dim modeldataset As New DataSet
    Dim sqlcommand As SqlCommandBuilder
    Dim newrow As DataRow
    Dim conn As New SqlConnection

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Try
            conn = GetConnect()
            conn.Open()

            sqldata = New SqlDataAdapter("SELECT * FROM PlaneTypes", conn)
            sqlcommand = New SqlCommandBuilder(sqldata)
            sqldata.InsertCommand = sqlcommand.GetInsertCommand()
            sqldata.Fill(modeldataset, "PlaneTypes")
            newrow = modeldataset.Tables("PlaneTypes").NewRow()
            newrow("Manufacturer") = txtMake.Text
            newrow("Model") = txtModel.Text
            newrow("NRows") = txtRows.Text
            newrow("NCols") = txtColumns.Text

            modeldataset.Tables("PlaneTypes").Rows.Add(newrow)
            sqldata.Update(modeldataset, "PlaneTypes")

        Catch ex As Exception
            MsgBox("SQL SERVER ERROR", MsgBoxStyle.Critical)
        Finally
            conn.Close()
        End Try
    End Sub
End Class