我试图通过单击表格行将datagrid值传递给另一个表单我的问题是,如果SELECT语句是正确的代码?
点击表格行>将结果显示为其他形式
编辑:我现在正在使用
这是我的新代码:
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
我运行程序时数据类型不匹配
答案 0 :(得分:0)
虽然拨打Form4.MetroGrid1.CurrentRow.Cells(0).Selected
是合法的,但可能无法访问Form4
的实例,而且我不认为.Selected
是您需要的价值该声明。最好为Sub New
制作Form6
将数据传递给它的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()
用法:
{{1}}