1

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!

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Charles
  • 11
  • 2
  • Since it is bound, you should be able to pass the entire related DataRow to the other form. Just created a method to receive it. Some of those DB provider objects should be used and disposed of each time. [See this](http://stackoverflow.com/a/29187199/1070452) – Ňɏssa Pøngjǣrdenlarp Jan 20 '16 at 23:01
  • You should bind your `DataTable` to a `BindingSource` and bind that to the grid. You can then get the current record from the `BindingSource` via its `Current` property. The object you get will be a `DataRowView`, which you can then pass to the editing dialogue. All you have to do is edit that `DataRowView` in the edit dialogue and the grid will automatically update. – jmcilhinney Jan 21 '16 at 00:35
  • @jmcilhinney : have you got any resources on how to actually do that? Would be really grateful. – Charles Jan 21 '16 at 16:31
  • What do you not understand about the resources you found when you looked for yourself? – jmcilhinney Jan 21 '16 at 23:15

0 Answers0