将 Datagridview 值传递给 VB.net 中的另一个表单
Passing Datagridview values to another form in VB.net
我试图通过单击 table 行将数据网格值传递给另一个表单 我的问题是,如果 SELECT 语句是正确的代码?
单击 table 行 > 以其他形式显示结果
编辑:我现在可以使用了
这是我的新代码:
Dim selectQ As String = "SELECT FirstName, LastName, MI, DOB, Age, Gender, Dept, DEP, MStatus, Phone1, Phone2, Tel, Email, HomeAdd, StreetAdd, CityAdd, Region, Refname1, Refname2, Refnum1, Refnum2 FROM Empinfo WHERE ID = " & Form4.MetroGrid1.CurrentRow.Cells(0).Value.ToString
之前:
Dim selectQ As String = "SELECT FirstName, LastName, MI, DOB, Age,
Gender, Dept, DEP, MStatus
FROM Empinfo
WHERE ID = '" & Form4.MetroGrid1.CurrentRow.Cells(0).Selected & "'"
'我的完整代码:
Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim constr As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\DB1.accdb"
Dim con As New OleDbConnection(constr)
Dim datareader As OleDbDataReader
Dim selectQ As String = "SELECT FirstName, LastName, MI, DOB, Age, Gender, Dept, DEP, MStatus FROM Empinfo WHERE ID = '" & Form4.MetroGrid1.CurrentRow.Cells(0).Selected & "'"
Dim cmd As New OleDbCommand(selectQ, con)
con.Open()
datareader = cmd.ExecuteReader
If datareader.HasRows Then
datareader.Read()
MetroTextBox1.Text = datareader(0).ToString
MetroTextBox2.Text = datareader(1).ToString
MetroTextBox3.Text = datareader(2).ToString
MetroDateTime1.Text = datareader(3).ToString
MetroComboBox1.Text = datareader(4).ToString
MetroComboBox2.Text = datareader(5).ToString
MetroComboBox3.Text = datareader(6).ToString
MetroDateTime2.Text = datareader(7).ToString
MetroComboBox5.Text = datareader(8).ToString
datareader.Close()
Else
MetroFramework.MetroMessageBox.Show(Me, "There is no data present", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
con.Close()
Catch ex As Exception
MsgBox(ex.ToString)
'MetroFramework.MetroMessageBox.Show(Me, ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
当我 运行 程序
时,我得到数据类型不匹配
虽然调用 Form4.MetroGrid1.CurrentRow.Cells(0).Selected
是合法的,但它可能不会访问 Form4
的实例,而且我认为 .Selected
不是语句所需的值。最好为 Form6
创建一个 Sub New
来将数据传递给它。
Public Class Form6
Private id As String
Public Sub New(id As String)
Me.id = id
End Sub
Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim constr As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\DB1.accdb"
Dim con As New OleDbConnection(constr)
Dim datareader As OleDbDataReader
Dim selectQ As String = "SELECT FirstName, LastName, MI, DOB, Age, Gender, Dept, DEP, MStatus FROM Empinfo WHERE ID = '" & id & "'"
Dim cmd As New OleDbCommand(selectQ, con)
con.Open()
datareader = cmd.ExecuteReader
If datareader.HasRows Then
datareader.Read()
MetroTextBox1.Text = datareader(0).ToString
MetroTextBox2.Text = datareader(1).ToString
MetroTextBox3.Text = datareader(2).ToString
MetroDateTime1.Text = datareader(3).ToString
MetroComboBox1.Text = datareader(4).ToString
MetroComboBox2.Text = datareader(5).ToString
MetroComboBox3.Text = datareader(6).ToString
MetroDateTime2.Text = datareader(7).ToString
MetroComboBox5.Text = datareader(8).ToString
datareader.Close()
Else
MetroFramework.MetroMessageBox.Show(Me, "There is no data present", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
con.Close()
Catch ex As Exception
MsgBox(ex.ToString)
'MetroFramework.MetroMessageBox.Show(Me, ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
用法:
Dim f6 As New Form6(MetroGrid1.CurrentRow.Cells(0).Value.ToString)
f6.Show()
我试图通过单击 table 行将数据网格值传递给另一个表单 我的问题是,如果 SELECT 语句是正确的代码?
单击 table 行 > 以其他形式显示结果
编辑:我现在可以使用了
这是我的新代码:
Dim selectQ As String = "SELECT FirstName, LastName, MI, DOB, Age, Gender, Dept, DEP, MStatus, Phone1, Phone2, Tel, Email, HomeAdd, StreetAdd, CityAdd, Region, Refname1, Refname2, Refnum1, Refnum2 FROM Empinfo WHERE ID = " & Form4.MetroGrid1.CurrentRow.Cells(0).Value.ToString
之前:
Dim selectQ As String = "SELECT FirstName, LastName, MI, DOB, Age,
Gender, Dept, DEP, MStatus
FROM Empinfo
WHERE ID = '" & Form4.MetroGrid1.CurrentRow.Cells(0).Selected & "'"
'我的完整代码:
Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim constr As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\DB1.accdb"
Dim con As New OleDbConnection(constr)
Dim datareader As OleDbDataReader
Dim selectQ As String = "SELECT FirstName, LastName, MI, DOB, Age, Gender, Dept, DEP, MStatus FROM Empinfo WHERE ID = '" & Form4.MetroGrid1.CurrentRow.Cells(0).Selected & "'"
Dim cmd As New OleDbCommand(selectQ, con)
con.Open()
datareader = cmd.ExecuteReader
If datareader.HasRows Then
datareader.Read()
MetroTextBox1.Text = datareader(0).ToString
MetroTextBox2.Text = datareader(1).ToString
MetroTextBox3.Text = datareader(2).ToString
MetroDateTime1.Text = datareader(3).ToString
MetroComboBox1.Text = datareader(4).ToString
MetroComboBox2.Text = datareader(5).ToString
MetroComboBox3.Text = datareader(6).ToString
MetroDateTime2.Text = datareader(7).ToString
MetroComboBox5.Text = datareader(8).ToString
datareader.Close()
Else
MetroFramework.MetroMessageBox.Show(Me, "There is no data present", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
con.Close()
Catch ex As Exception
MsgBox(ex.ToString)
'MetroFramework.MetroMessageBox.Show(Me, ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
当我 运行 程序
时,我得到数据类型不匹配虽然调用 Form4.MetroGrid1.CurrentRow.Cells(0).Selected
是合法的,但它可能不会访问 Form4
的实例,而且我认为 .Selected
不是语句所需的值。最好为 Form6
创建一个 Sub New
来将数据传递给它。
Public Class Form6
Private id As String
Public Sub New(id As String)
Me.id = id
End Sub
Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim constr As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\DB1.accdb"
Dim con As New OleDbConnection(constr)
Dim datareader As OleDbDataReader
Dim selectQ As String = "SELECT FirstName, LastName, MI, DOB, Age, Gender, Dept, DEP, MStatus FROM Empinfo WHERE ID = '" & id & "'"
Dim cmd As New OleDbCommand(selectQ, con)
con.Open()
datareader = cmd.ExecuteReader
If datareader.HasRows Then
datareader.Read()
MetroTextBox1.Text = datareader(0).ToString
MetroTextBox2.Text = datareader(1).ToString
MetroTextBox3.Text = datareader(2).ToString
MetroDateTime1.Text = datareader(3).ToString
MetroComboBox1.Text = datareader(4).ToString
MetroComboBox2.Text = datareader(5).ToString
MetroComboBox3.Text = datareader(6).ToString
MetroDateTime2.Text = datareader(7).ToString
MetroComboBox5.Text = datareader(8).ToString
datareader.Close()
Else
MetroFramework.MetroMessageBox.Show(Me, "There is no data present", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
con.Close()
Catch ex As Exception
MsgBox(ex.ToString)
'MetroFramework.MetroMessageBox.Show(Me, ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
用法:
Dim f6 As New Form6(MetroGrid1.CurrentRow.Cells(0).Value.ToString)
f6.Show()