我没有从以下代码中得到我想要的结果:
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数据类型不兼容。有什么想法吗?
答案 0 :(得分:2)
您正在将SelectdRows对象转换为字符串,该字符串可能会返回like "System.Windows.Forms.DataGridViewSelectedRowCollection"
个字符。执行st(0)
将从该字符串中获取第一个字符串,即“S”。
也许你应该试试这个:
Dim st As String = tblView.SelectedRows[0].Cells[0].Value.ToString
txtID.Text = st
答案 1 :(得分:1)
试试这个
txtID.text = myReader.Item(0)
txtRack.text = myReader.Item(1)
或
txtID.text = myReader.Item("Fieldname1")
txtRack.text = myReader.Item("Fieldname2")