以新格式

时间:2016-01-20 22:57:16

标签: vb.net winforms ms-access datagridview

我有一个管理系统,其中有DataGridView显示记录的表单。 我希望能够点击一行并点击“编辑”按钮,新的“编辑表单”应该会显示所有记录详细信息。

数据网格网格仅使用当前选择语句显示7/21字段周围ms访问的一些字段。

单击编辑时,如何以新的形式显示所有字段及其详细信息?

这是我的DBControl类:

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

我当前的编辑表单:

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

,最后是我的编辑按钮

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

谢谢大家!

0 个答案:

没有答案