Frans_Truyens
asked on
An updateble datagridview in a a Winforms application
I want to make a datagridview on a form in a winforms application. This datagridview has to be linked to a MS Access table, and it has to be updatable. So I need the operations Add, Edit and Delete.
to bind to datagridview:
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\myDB.mdb")
Dim cmd As OleDbCommand = New OleDbCommand("Select * FROM User", con)
con.Open()
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "MyTable")
DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
con.Close()
con = Nothing
ASKER
I know how to fill a datagridview with an MS Access query. But when someone changes the datagrid: edits, deletes or adds something, I want these changes to be stored in the table.
Take a look at the sample code here...
http://www.daniweb.com/forums/thread129438.html
http://www.daniweb.com/forums/thread129438.html
ASKER
This is my code. I am new to Winforms, could you complete the SaveGridToTable?
Public Class Teksten
Private Sub Teksten_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strSQL As String
strSQL = "SELECT Taal, Nummer, Tekst FROM Tekst ORDER BY Nummer, Taal;"
Dim adp As New OleDb.OleDbDataAdapter(strSQL, GetConnection)
Dim stPersoon As New DataSet
adp.Fill(stPersoon, "Teksten")
Me.gridTeksten.DataSource = stPersoon.Tables("Teksten")
'me.gridTeksten.Columns("ID").Visible = False
End Sub
Private Sub SaveGridToTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
End Class
Ok, this should do it for you...
(Have to make your dataadaptor and dataset global too)
(Have to make your dataadaptor and dataset global too)
Dim adp As OleDb.OleDbDataAdapter
Dim stPersoon As DataSet
Private Sub Teksten_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strSQL As String
strSQL = "SELECT Taal, Nummer, Tekst FROM Tekst ORDER BY Nummer, Taal;"
adp = New OleDb.OleDbDataAdapter(strSQL, GetConnection)
stPersoon = New DataSet
adp.Fill(stPersoon, "Teksten")
Me.gridTeksten.DataSource = stPersoon.Tables("Teksten")
'me.gridTeksten.Columns("ID").Visible = False
End Sub
Private Sub SaveGridToTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cmdbuilder As New OleDb.OleDbCommandBuilder(adp)
Dim i As Integer
Try
i = adp.Update(stPersoon, "trial1")
MsgBox("Records Updated= " & i)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
My bad, forgot to change a name...
Dim adp As OleDb.OleDbDataAdapter
Dim stPersoon As DataSet
Private Sub Teksten_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strSQL As String
strSQL = "SELECT Taal, Nummer, Tekst FROM Tekst ORDER BY Nummer, Taal;"
adp = New OleDb.OleDbDataAdapter(strSQL, GetConnection)
stPersoon = New DataSet
adp.Fill(stPersoon, "Teksten")
Me.gridTeksten.DataSource = stPersoon.Tables("Teksten")
'me.gridTeksten.Columns("ID").Visible = False
End Sub
Private Sub SaveGridToTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cmdbuilder As New OleDb.OleDbCommandBuilder(adp)
Dim i As Integer
Try
i = adp.Update(stPersoon, "Teksten")
MsgBox("Records Updated= " & i)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
ASKER
The adding of records works fine. But the update and the delete returns the following error:
"Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information."
I have a unique key, an autonumber field, but it is not in the SQL string
"Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information."
I have a unique key, an autonumber field, but it is not in the SQL string
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have a primary key! It is ID, it is an autonumber.
ASKER
I added the ID and everything works fine. Thanks a lot.
ASKER
Thanks a lot. This solved my problem.
for Access 2007, please refer to the connection string as below:
Provider=Microsoft.ACE.OLE
Open in new window