Click here to Skip to main content
15,897,519 members

How can update datagridview cell content when changes are made into excel 2013 database

God is real asked:

Open original thread
Imports System.Data.OleDb
Public Class FormAttacheeInfo
    Dim con As New OleDbConnection

   Private Sub FormAttacheeInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.AllowUserToAddRows = False
        con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\TONYMA\Documents\RegistrationDB.accdb"
        con.Open()
        Dim dt As New DataTable
        Dim ds As New DataSet
        ds.Tables.Add(dt)
        Dim da As New OleDbDataAdapter
        da = New OleDbDataAdapter("SELECT * FROM TblAttachee", con)
        da.Fill(dt)
        DataGridView1.DataSource = dt.DefaultView
        con.Close()
    End Sub


    Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click
        If DataGridView1.SelectedCells.Count > 0 Then
            If MessageBox.Show("You want to Makes changes to this Record ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
                Dim dt As New DataTable
                Dim ds As New DataSet
                ds.Tables.Add(dt)
                Dim da As New OleDbDataAdapter
                con.Open()
                da = New OleDbDataAdapter("SELECT * FROM TblAttachee", con)
                da.Fill(dt)

                Dim i As Integer = DataGridView1.SelectedCells(0).IsInEditMode
                dt.Rows(i).BeginEdit()
                'which code am i missing here so as to update changes into database when cell content has been changed

               
                dt.Rows(i).EndEdit()

                'code for refreshing datagridview1 that user can see the updated cell content

                con.Close()
                MessageBox.Show("Attachee's record successfully updated", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                Me.Focus()
            End If
        End If
          
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        If DataGridView1.SelectedRows.Count > 0 Then
            If MessageBox.Show("You want to Clear this Record ?", "ATTACHEES RECORD", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
                Dim dt As New DataTable
                Dim ds As New DataSet
                ds.Tables.Add(dt)
                Dim da As New OleDbDataAdapter
                con.Open()
                da = New OleDbDataAdapter("SELECT * FROM TblAttachee", con)
                da.Fill(dt)
                Dim i As Integer = DataGridView1.SelectedRows(0).Index
                dt.Rows(i).BeginEdit()
                DataGridView1.Rows.Remove(DataGridView1.SelectedRows(0))
                dt.Rows(i).Delete()
                dt.Rows(i).EndEdit()
                Dim cmd As New OleDbCommandBuilder(da)
                ' Can't understand this exception , guys please help with a delete code ?
                da.Update(dt)

                DataGridView1.DataSource = dt.DefaultView
                con.Close()
                MessageBox.Show("Attachee deleted", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                Me.Focus()
            End If
        End If
    End Sub
End Class


What I have tried:

i have tried even google . NO ANSWER SO FAR
Tags: Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900