I have a management system which has a form with DataGridView
showing records.
I want to be able to click on a row and click on a 'edit' button, new 'edit form' should come up with all of the record details.
The datagrid grid only shows a few of the fields from ms access around 7/21 fields using of course select statement.
How do I show all of the fields and its details in a new form when I click edit?
This is my DBControl class:
Imports System.Data.OleDb
Public Class DBControl
'create db connection
Private DBCon As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=IPM_DB.accdb;")
'prepare db command
'variable for database commands
Private DBcmd As OleDbCommand
'db data
'a place to store our data
'storing and handling
'public because will be accessed from main form
'data adapter can perform select, insert, update and elete SQL operations in data source
'executing commands against database, perform actions ie filling data table
Public DBDA As OleDbDataAdapter
'data table
Public DBDT As DataTable
'query parameters
Public Params As New List(Of OleDbParameter)
'query statistics
Public RecordCount As Integer
Public Exception As String
Public Sub ExecQuery(Query As String)
'reset query statistics
RecordCount = 0
Exception = ""
Try
'open a connection
DBCon.Open()
'create db command
DBcmd = New OleDbCommand(Query, DBCon)
'load paramsinto db command
Params.ForEach(Sub(p) DBcmd.Parameters.Add(p))
'clear params list
Params.Clear()
'execute command and fill datatable
DBDT = New DataTable
DBDA = New OleDbDataAdapter(DBcmd)
RecordCount = DBDA.Fill(DBDT)
Catch ex As Exception
Exception = ex.Message
End Try
'close connection
If DBCon.State = ConnectionState.Open Then DBCon.Close()
End Sub
'include query & command parameters
Public Sub AddParam(Name As String, Value As Object)
Dim NewParam As New OleDbParameter(Name, Value)
Params.Add(NewParam)
End Sub
End Class
my current edit form:
Public Class Edit_Incident_Record
Privte Access As New DBControl
Private CurrentRecord As Integer = 0 'Incident.dgvData.SelectedRows(0).Index
Private Sub Edit_Incident_Record_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim currID As Integer = Incident.dgvData.SelectedRows(0).Cells("IncidentID").Value.ToString()
Access.ExecQuery("SELECT * from Incidents where IncidentID = '" & currID & "'")
Dim r As DataRow = Access.DBDT.Rows(currID)
'Access.ExecQuery("SELECT * from Incidents where IncidentID = '" & currID & "'")
txtIncidentID.Text = currID
'txtSummary.Text = r("Summary").ToString
End Sub
End Class
and finally my edit button
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
If Me.dgvData.SelectedRows.Count = 0 Or Me.dgvData.SelectedRows.Count > 1 Then
MessageBox.Show("Select just one row")
Return
End If
Edit_Incident_Record.Show()
End Sub
Thank You all!