Vb.net 以新形式编辑DataGridView中的选定行
我有一个管理系统,它有一个带有Vb.net 以新形式编辑DataGridView中的选定行,vb.net,winforms,ms-access,datagridview,Vb.net,Winforms,Ms Access,Datagridview,我有一个管理系统,它有一个带有DataGridView显示记录的表单。 我想能够点击一行并点击“编辑”按钮,新的“编辑表单”应该会显示所有记录的详细信息 datagrid网格仅显示MSAccess中的几个字段,这些字段大约是7/21字段,当然使用select语句 单击“编辑”时,如何在新表单中显示所有字段及其详细信息 这是我的DBControl类: Imports System.Data.OleDb Public Class DBControl 'create db connection Pr
DataGridView
显示记录的表单。
我想能够点击一行并点击“编辑”按钮,新的“编辑表单”应该会显示所有记录的详细信息
datagrid网格仅显示MSAccess中的几个字段,这些字段大约是7/21字段,当然使用select语句
单击“编辑”时,如何在新表单中显示所有字段及其详细信息
这是我的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
我当前的编辑表单:
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
谢谢大家! 因为它是绑定的,所以您应该能够将整个相关数据行传递给另一个表单。刚刚创建了一个方法来接收它。每次都应该使用和处理一些DB提供程序对象。您应该将
DataTable
绑定到BindingSource
并将其绑定到网格。然后,您可以通过current
属性从BindingSource
获取当前记录。您获得的对象将是一个数据行视图
,然后您可以将其传递到编辑对话框。您所要做的就是在编辑对话框中编辑DataRowView
,网格将自动更新。@jmcilhinney:您有关于如何实际执行此操作的资源吗?非常感谢。当您自己查找时,您对找到的资源有什么不了解的?因为它是绑定的,所以您应该能够将整个相关数据行传递给另一个表单。刚刚创建了一个方法来接收它。每次都应该使用和处理一些DB提供程序对象。您应该将DataTable
绑定到BindingSource
并将其绑定到网格。然后,您可以通过current
属性从BindingSource
获取当前记录。您获得的对象将是一个数据行视图
,然后您可以将其传递到编辑对话框。您所要做的就是在编辑对话框中编辑DataRowView
,网格将自动更新。@jmcilhinney:您有关于如何实际执行此操作的资源吗?我真的很感激。当你寻找自己的时候,你对你所找到的资源有什么不了解的?