Updating my datagrid

David_nyh

Member
Joined
Mar 4, 2005
Messages
16
Programming Experience
Beginner
How can I save my datagrid? I tried everything. I can delete a row and cancel it but I can't save it.

Some of my code:
VB.NET:
'Data van de tabel parameters ophalen
Private Sub fillDataSetParameters(ByVal paramSQLString As String)

'Data uit de tabel parameters ophalen
Dim paramDataSet As New DataSet()
Dim paramDataAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter()
Dim paramCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand(paramSQLString)
paramCommand.CommandType = CommandType.Text
paramCommand.Connection() = myDbConnection
paramDataAdapter.SelectCommand = paramCommand
paramDataAdapter.TableMappings.Add("Table", "tabel_parameters")
paramDataAdapter.Fill(paramDataSet)

'Maximum aantal boeken en maximum aantal weken dat boeken mogen ontleend worden
lblMaxOntlBoek.DataBindings.Add("text", paramDataSet, "tabel_parameters.max_aant_boeken")
lblMaxOntlWeken.DataBindings.Add("text", paramDataSet, "tabel_parameters.max_ontl_tijd")

End Sub

'Data van de tabel leden ophalen
Private Sub fillDataSetLeden(ByVal ledenSQLString As String)

'Data uit de tabel leden ophalen
Dim ledenCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand(ledenSQLString, myDbConnection)
Dim ledenReader As OleDb.OleDbDataReader = ledenCommand.ExecuteReader()

'Verwijder bestaande items in de ListView
lstLeden.Items.Clear()

'Lees alle rijen van de data
Dim ledenListViewItem As ListViewItem
While ledenReader.Read() = True
Try
'Voeg de items toe aan de ListView
ledenListViewItem = New ListViewItem()
ledenListViewItem.Text = ledenReader.Item("lid_id")
ledenListViewItem.SubItems.Add(ledenReader.Item("lid_voornaam"))
ledenListViewItem.SubItems.Add(ledenReader.Item("lid_naam"))
lstLeden.Items.Add(ledenListViewItem)
ledenListViewItem = Nothing
Catch
'???
End Try
End While
ledenReader.Close()

'Zet det focus op de listview en zorg ervoor dat de eerste rij actief is
lstLeden.Focus()
lstLeden.Items(0).Selected = True

End Sub

'Data van de tabel ontleningen ophalen
Private Sub loadIndividual()

Dim ontlSQLString As String
Dim ontlName As String
Dim ontlDataRow As DataRow
Dim oCtl As Object

ontlDataSet.Clear()

If Me.lstLeden.SelectedItems.Count > 0 Then
Try
'Data uit de tabel ontlening ophalen
ontlSQLString = "SELECT tabel_ontlening.ontl_id, tabel_boeken.boek_titel, tabel_ontlening.ontl_datum, tabel_ontlening.ontl_terug_datum FROM tabel_boeken INNER JOIN tabel_ontlening ON tabel_boeken.boek_id = tabel_ontlening.boek_id Where lid_id = " & Me.lstLeden.SelectedItems(0).Text
Dim ontlDataAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter()
Dim ontlCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand(ontlSQLString)
ontlCommand.CommandType = CommandType.Text
ontlCommand.Connection() = myDbConnection
ontlDataAdapter.SelectCommand = ontlCommand
ontlDataAdapter.TableMappings.Add("Table", "tabel_ontlening")
ontlDataAdapter.Fill(ontlDataSet)

'Data binden aan de DataGrid
DataGrid1.SetDataBinding(ontlDataSet, "tabel_ontlening")

'Een stijl aan de DataGrid koppelen
Dim tableStyle As New DataGridTableStyle()
DataGrid1.TableStyles.Clear()
tableStyle.MappingName = "tabel_ontlening"

Dim column As New DataGridTextBoxColumn()
column.MappingName = "ontl_id"
column.HeaderText = "ID"
column.Width = 25
tableStyle.GridColumnStyles.Add(column)

column = New DataGridTextBoxColumn()
column.MappingName = "boek_titel"
column.HeaderText = "Boek titel"
column.Width = 304
tableStyle.GridColumnStyles.Add(column)

column = New DataGridTextBoxColumn()
column.MappingName = "ontl_datum"
column.HeaderText = "Datum ontleend"
column.Width = 110
tableStyle.GridColumnStyles.Add(column)

column = New DataGridTextBoxColumn()
column.MappingName = "ontl_terug_datum"
column.HeaderText = "Datum terug "
column.Width = 110
tableStyle.GridColumnStyles.Add(column)

DataGrid1.TableStyles.Add(tableStyle)

Catch oexpData As OleDb.OleDbException
MessageBox.Show("Error loading individual data: " & oexpData.Message)
Exit Sub
End Try

For Each oCtl In Me.Controls
If TypeOf oCtl Is TextBox Then
ontlName = Mid(oCtl.Name, 4)
Try
oCtl.text = ontlDataRow(ontlName).ToString
Catch oexp As Exception
End Try
End If
Next
End If

End Sub

'Verwijder de geselecteerde boek uit de DataGrid
Private Sub btnVerwijder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerwijder.Click

Dim Response As VariantType
Response = MessageBox.Show("Confirm Delete Record?", "Delete Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
If (Response = vbYes) Then
StatusBar1.Panels(0).Text = "Deleting..."
If (Me.BindingContext(ontlDataSet, "tabel_ontlening").Count > 0) Then
Me.BindingContext(ontlDataSet, "tabel_ontlening").RemoveAt(Me.BindingContext(ontlDataSet, "tabel_ontlening").Position)
Me.updateLabel()
Me.btnAllesAnuleren.Enabled = True
Me.btnAllesOpslaan.Enabled = True
End If
End If

End Sub

'Maak de wijzigingen ongedaan in de DataGrid
Private Sub btnAllesAnuleren_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAllesAnuleren.Click

Me.ontlDataSet.RejectChanges()
Me.updateLabel()
Me.btnAllesAnuleren.Enabled = False

End Sub

'Slaag alle wijzigingen in de DataGrid op
Private Sub btnAllesOpslaan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAllesOpslaan.Click
Me.ontlDataSet.AcceptChanges()
End Sub
 

DavidT_macktool

Well-known member
Joined
Oct 21, 2004
Messages
502
Location
Indiana
Programming Experience
3-5
Think "I need to update my datasource". The data grid is displaying a dataset, the dataset update command will update it. Use the dataform wizard to show you how it can easiley be done. The dataform wizard will create an update button and all the code necessary to get you going.

check out this thread: http://www.vbdotnetforums.com/showthread.php?t=1999
 
Last edited:
Top Bottom