VB.net 组合框显示 System.Data.DataRowView
VB.net combobox displaying System.Data.DataRowView
我正在尝试通过 Access 2013 return 来自 SQL 查询的数据到我的 VB 表单中,我的代码如下。
Private Sub frm_8_IncidentResponseSelect_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable
Dim query As String = "select ([response ID] &' '&[response date]) As responsedetails from response where [incident id] = " & txt_incidentid.Text & " "
Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\MyDatabase"
Using command As New OleDbCommand(query, connection)
Using adapter As New OleDbDataAdapter(command)
connection.Open()
adapter.Fill(dt)
connection.Close()
End Using
End Using
End Using
For Each Data As DataRow In dt.Rows
If dt.Rows.Count > 0 Then
cb_responseid.DataSource = dt
cb_responseid.DisplayMember = "[responsedetails]"
cb_responseid.ValueMember = "[response id]
End If
Next
End Sub
我首先遇到了两个错误
VB return 出现以下错误消息
'Cannot bind to the new display member'
并突出显示以下代码行
cb_responseid.ValueMember = "[响应编号]
而且组合框中的任何项目都 returning 为 System.Data.DataRowView
谁能告诉我 how/why 我的代码有误吗?
首先,您应该删除 table 行上的循环,只需将 DataTable 设置为您的数据源并删除用于 DisplayMember
和 [=13= 的字符串周围的方括号] 属性
If dt.Rows.Count > 0 Then
cb_responseid.DataSource = dt
cb_responseid.DisplayMember = "responsedetails"
cb_responseid.ValueMember = "responseid"
End If
然后您需要将查询更改为 return 以及 [response id]
字段,因为(在将其重命名为不带空格的内容之后)。您的代码使用该字段组成一个名为 responsedetails
的新字段,因此它作为用于 ValueMember
的源字段不再可见
Dim query As String = "select [response id] as responseid, " & _
" ([response ID] &' '&[response date]) As responsedetails " & _
" from response where [incident id] = " & txt_incidentid.Text & " "
也就是说,请记住构建命令文本连接字符串是数据库编程中最糟糕的事情。您应该学习如何使用参数化查询
我要更改的另一件事是使用这些字段中的空格。它们是一种非常嘈杂的干扰,造成的麻烦多于它们解决的问题。
我正在尝试通过 Access 2013 return 来自 SQL 查询的数据到我的 VB 表单中,我的代码如下。
Private Sub frm_8_IncidentResponseSelect_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable
Dim query As String = "select ([response ID] &' '&[response date]) As responsedetails from response where [incident id] = " & txt_incidentid.Text & " "
Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\MyDatabase"
Using command As New OleDbCommand(query, connection)
Using adapter As New OleDbDataAdapter(command)
connection.Open()
adapter.Fill(dt)
connection.Close()
End Using
End Using
End Using
For Each Data As DataRow In dt.Rows
If dt.Rows.Count > 0 Then
cb_responseid.DataSource = dt
cb_responseid.DisplayMember = "[responsedetails]"
cb_responseid.ValueMember = "[response id]
End If
Next
End Sub
我首先遇到了两个错误
VB return 出现以下错误消息
'Cannot bind to the new display member' 并突出显示以下代码行 cb_responseid.ValueMember = "[响应编号]
而且组合框中的任何项目都 returning 为 System.Data.DataRowView
谁能告诉我 how/why 我的代码有误吗?
首先,您应该删除 table 行上的循环,只需将 DataTable 设置为您的数据源并删除用于 DisplayMember
和 [=13= 的字符串周围的方括号] 属性
If dt.Rows.Count > 0 Then
cb_responseid.DataSource = dt
cb_responseid.DisplayMember = "responsedetails"
cb_responseid.ValueMember = "responseid"
End If
然后您需要将查询更改为 return 以及 [response id]
字段,因为(在将其重命名为不带空格的内容之后)。您的代码使用该字段组成一个名为 responsedetails
的新字段,因此它作为用于 ValueMember
Dim query As String = "select [response id] as responseid, " & _
" ([response ID] &' '&[response date]) As responsedetails " & _
" from response where [incident id] = " & txt_incidentid.Text & " "
也就是说,请记住构建命令文本连接字符串是数据库编程中最糟糕的事情。您应该学习如何使用参数化查询
我要更改的另一件事是使用这些字段中的空格。它们是一种非常嘈杂的干扰,造成的麻烦多于它们解决的问题。