What I've learned so far:
Databinding using the automatic binding in VB.NET is a mess. It is simpler, in a way, to just query the database yourself, save that to a dataset/table somewhere, and referance the stored table whenever you need data.
1. To move the Access database and still have the .NET program connect:
Use My.Settings.whatever in order to store the filepath.
- 'Change database location
-
Private Sub btnDbChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDbChange.Click
-
'I have no idea... Where did I even find this code online??
-
Dim fdlg As OpenFileDialog = New OpenFileDialog()
-
fdlg.Title = "Select the database"
-
fdlg.InitialDirectory = "c:\"
-
fdlg.Filter = "Access 2007 database files (*.accdb)|*.accdb"
-
fdlg.FilterIndex = 1
-
fdlg.RestoreDirectory = True
-
If fdlg.ShowDialog() = DialogResult.OK Then
-
Me.txtDbLoca.Text = fdlg.FileName
-
End If
-
End Sub
2. How to query an Access database to get a table?
- Public Function FillFromAccess(ByVal QueryString As String, ByVal FillTable As String) As DataSet
-
'Get data from a local Access database
-
Dim OLEConnString As String
-
OLEConnString = "Provider=" & My.Settings.Connection_AccessProvider & ";" _
-
& "Data Source=" & My.Settings.Connection_Location & ";"
-
-
Dim DBConn As New OleDbConnection(OLEConnString)
-
Dim DBCommand As OleDbDataAdapter
-
Dim dataset_Access As New DataSet
-
-
'Pull a table out of the database.
-
DBCommand = New OleDb.OleDbDataAdapter(QueryString, DBConn)
-
Try 'Put that into an object
-
DBCommand.Fill(dataset_Access, FillTable)
-
Catch ex As Exception
-
FillFromAccess = Nothing
-
Exit Function
-
End Try
-
-
'Send the dataset back to the sub that called this function!
-
Return dataset_Access
-
End Function
3. Update the database when I need to make changes?
- Public Sub SendToAccess(ByVal QueryString As String)
-
'Insert, Update, or Delete data in a local Access Database table
-
-
'http://www.eggheadcafe.com/community/aspnet/14/70996/insert-into-statement-ol.aspx
-
Dim DBCommand As New OleDb.OleDbCommand(QueryString, New OleDb.OleDbConnection("Provider=" & My.Settings.Connection_AccessProvider & ";" & "Data Source=" & My.Settings.Connection_Location & ";"))
-
-
DBCommand.Connection.Open()
-
Try
-
DBCommand.ExecuteNonQuery()
-
Catch ex As Exception
-
End Try
-
DBCommand.Connection.Close()
-
DBCommand.Dispose()
-
DBCommand = Nothing
-
End Sub
4. How do I find the ID of a record when it is in a DataGridView?
- '* Find if a row is selected
-
'http://msdn.microsoft.com/en-us/library/x8x9zk5a.aspx
-
Dim selectedRowCount As Integer = Me.dgvTasks.Rows.GetRowCount(DataGridViewElementStates.Selected)
-
If selectedRowCount <= 0 Then Exit Sub
-
-
' Task ID
-
Try
-
intTaskID = Me.dgvTasks.SelectedRows(0).Cells.Item("ID").Value
-
Catch ex As Exception
-
End Try
5. Why not use the ListView instead of the DataGridView?
Well... a ListView requires you to do more coding to make it look and behave correctly. AND - you can change a dgv to look like a ListView. Look for the properties AutoSizeColumnsMode, GridColor, and SelectionMode. If you put this code in a public module, you will be able to change all DataGridViews to look the same, with one line of code for each, when it's form loads:
- Public Sub dgvRowColors(ByVal dgv As DataGridView)
-
'http://www.dotnetspider.com/resources/23026-GridView-FAQ-s.aspx
-
With dgv
-
.RowsDefaultCellStyle.BackColor = Color.White
-
.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray
-
.GridColor = Color.Gray
-
End With
-
End Sub
That's all! I hope that someone finds this and puts it to use. Please leave a comment here if this helped you.