why can't update records on datagridview???

kank

Active member
Joined
Dec 12, 2011
Messages
26
Programming Experience
Beginner
When I keyin the new row on datagridview and then click another row, the row that just keyed in was disappeared(as on the pictures I attached) 1.png2.png , don't know why so I can't click save to save record in sql server :-(



pls help, I am newbie, thanks a lot
 
Yes, it is bound. I filter the data and show only position and department. Then, add new record of position and department, then click another row, it was disappeared.

I have tried to add new record by not filter, it show as the attach. Confuse??? Why

before_save.pngafterSave.png

The below is all my code on this form. :-(

VB.NET:
Imports System.Data.SqlClient
Imports System.Data.OleDb

Public Class fNew
    Private da As New SqlDataAdapter
    Private bsource As BindingSource = New BindingSource()
    Dim sql As String = "SELECT * FROM tblNew_IDL "
    Dim dt As DataTable = ds.Tables("New")
    Dim sect As String
    Dim dept As String
    
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        sect = cmbSect.Text
        dept = cmbDept.Text
        sqlconn.Open()
        If sect = Nothing Then
            MessageBox.Show("Please input Section")
            sqlconn.Close()
            Exit Sub
        End If
        If dept = Nothing Then
            MessageBox.Show("Please input Department")
            sqlconn.Close()
            Exit Sub
        End If

    
        da = New SqlDataAdapter(sql, sqlconn)
        ds = New DataSet()
        da.Fill(ds, "New")
        bsource.DataSource = ds.Tables("New")


        'To filter base on selected section and department
        bsource.Filter = "section='" & sect & "' And department='" & dept & "'"


        Me.DataGridView1.DataSource = bsource
        Me.DataGridView1.Columns(0).Visible = False
        Me.DataGridView1.Columns(1).Visible = False
        Me.DataGridView1.Columns(2).Visible = False
        sqlconn.Close()

        

    End Sub

    Private Sub fNew_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        sqlconn.ConnectionString = connString


        da = New SqlDataAdapter(sql, sqlconn)
        ds = New DataSet()
        'Dim commandBuilder As SqlCommandBuilder = New SqlCommandBuilder(da)
        da.Fill(ds, "New")
        bsource.DataSource = ds.Tables("New")
        Me.DataGridView1.DataSource = bsource
        'Me.DataGridView1.Columns(0).Visible = False
        'Me.DataGridView1.Columns(1).Visible = False
        'Me.DataGridView1.Columns(2).Visible = False
       
      
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        ' Update the database with the user's changes.       
        sqlconn.Open()
        Try

            Me.DataGridView1.BindingContext(dt).EndCurrentEdit()
            Me.da.Update(dt)

        Catch ex As SqlException
            MsgBox("Error updating the table" + ex.Message)
            sqlconn.Close()
            Exit Sub
        End Try

        MessageBox.Show("Table is updated sucessful!")
    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        sect = cmbSect.Text
        dept = cmbDept.Text
        sqlconn.Open()
        If sect = Nothing Then
            MessageBox.Show("Please input Section")
            sqlconn.Close()
            Exit Sub
        End If
        If dept = Nothing Then
            MessageBox.Show("Please input Department")
            sqlconn.Close()
            Exit Sub
        End If


        da = New SqlDataAdapter(sql, sqlconn)
        ds = New DataSet()
        da.Fill(ds, "New")
        bsource.DataSource = ds.Tables("New")


        'To filter base on selected section and department
        bsource.Filter = "section='" & sect & "' And department='" & dept & "'"


        Me.DataGridView1.DataSource = bsource
        Me.DataGridView1.Columns(0).Visible = False
        Me.DataGridView1.Columns(1).Visible = False
        Me.DataGridView1.Columns(2).Visible = False
    End Sub
End Class
 
Back
Top