双击VB.NET中的任何DataGridView行以在表单控件中显示结果

双击VB.NET中的任何DataGridView行以在表单控件中显示结果,vb.net,datagridview,Vb.net,Datagridview,我没有从以下代码中获得想要的结果: Private Sub tblView_SelectionChanged(ByVal sender As System.oject, ByVal e as System.EventArgs)Handles tblView.SelectionChanged Dim st As String = tblView.SelectedRows.ToString txtID.Text = st(0) Try

我没有从以下代码中获得想要的结果:

    Private Sub tblView_SelectionChanged(ByVal sender As System.oject, ByVal e as System.EventArgs)Handles tblView.SelectionChanged
        Dim st As String = tblView.SelectedRows.ToString
        txtID.Text = st(0)
        Try
           myCommand = New SqlCommand("SELECT * FROM tblRack WHERE RackID = "& txtID.Text &"", myConnection)
           myReader = myCommand.ExecuteReader()
           While myReader.Read
               txtID.Text = myReader(0).ToString
               txtRack.Text = myReader(1).ToString
           End While
           myReader.Close()
        Catch ex As Exception
           MsgBox (ex.Message)
        End Try
 End Sub

我希望在datagridview行上选择的数据显示在文本框中,但它会显示错误消息,即st(0)生成的“S”与我的RackID数据类型不兼容。有什么想法吗?

您正在将SelectdRows对象转换为字符串,该字符串可能会返回类似“System.Windows.Forms.DataGridViewSelectedRowCollection”之类的
。执行
st(0)
将从此字符串中获取第一个字符,即“S”

也许你应该试试这个:

    Dim st As String = tblView.SelectedRows[0].Cells[0].Value.ToString
    txtID.Text = st

您正在将SelectdRows对象转换为字符串,该字符串可能会返回类似“System.Windows.Forms.DataGridViewSelectedRowCollection”之类的
。执行
st(0)
将从此字符串中获取第一个字符,即“S”

也许你应该试试这个:

    Dim st As String = tblView.SelectedRows[0].Cells[0].Value.ToString
    txtID.Text = st
试试这个

txtID.text = myReader.Item(0)
txtRack.text = myReader.Item(1)

试试这个

txtID.text = myReader.Item(0)
txtRack.text = myReader.Item(1)


伙计,这没用。它显示错误“identifier expected”@tepkenvannkorn:这不应该由这两行引起。您在哪一行得到错误?我终于得到了答案:Private Sub tblView_CellClick(ByVal sender作为对象,ByVal e作为System.Windows.Forms.DataGridViewCellEventArgs)处理tblView.CellClick Dim I作为整数I=tblView.CurrentRow.Index txtID.Text=tblView.Item(0,I).Value txtRack.Text=tblView.Item(1,i)。Value End Sub i已测试,且有效。谢谢你,伙计,伙计,这没用。它显示错误“identifier expected”@tepkenvannkorn:这不应该由这两行引起。您在哪一行得到错误?我终于得到了答案:Private Sub tblView_CellClick(ByVal sender作为对象,ByVal e作为System.Windows.Forms.DataGridViewCellEventArgs)处理tblView.CellClick Dim I作为整数I=tblView.CurrentRow.Index txtID.Text=tblView.Item(0,I).Value txtRack.Text=tblView.Item(1,i)。Value End Sub i已测试,且有效。谢谢你,伙计。。。