我正在尝试用数据库中的数据填充组合框 - 使用sql。我正在尝试使用来自多个字段的信息(名字,姓氏)。我需要程序来连接字段并添加' FullName'进入组合框。这是我正在使用的代码 - 我得到一个错误,因为名称应该显示在" System.Data.DataRowView" :
Private Sub frmViewEmployee_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con.Open()
sqlstatement = "SELECT [First Name], [Last Name] FROM [EmployeeAccounts]"
da = New OleDb.OleDbDataAdapter(sqlstatement, con)
da.Fill(ds, "EmployeeName")
Dim x As Short = 0
Dim FirstName As String
Dim LastName As String
Dim MaxRows As Short = ds.Tables("EmployeeName").Rows.Count
Dim FullName(MaxRows) As String
x = 0
While x < MaxRows
FirstName = ds.Tables("EmployeeName").Rows(x).Item(0)
LastName = ds.Tables("EmployeeName").Rows(x).Item(1)
FullName(x) = FirstName & " " & LastName
x = x + 1
End While
comEmployeeName.DataSource = ds.Tables(CStr("EmployeeName"))
comEmployeeName.DisplayMember = CStr(FullName(x))
comEmployeeName.ValueMember = CStr(FullName(x))
End Sub
任何答案都应该受到赞赏 - 无论是纠正还是新代码。
答案 0 :(得分:0)
也许尝试更改你的sql:
"SELECT [First Name] + ' ' + [Last Name] As Fullname FROM [EmployeeAccounts]"
然后将Fullname设置为您的显示成员。
答案 1 :(得分:0)
"SELECT [First Name] & ' ' & [Last Name] As Fullname FROM [EmployeeAccounts]"
将你的加号改为&amp;因为vb.net喜欢他们+
答案 2 :(得分:0)
我会将此列设为隐藏列,然后使用连接字符串生成另一列。
当您在组合框上设置显示成员字段时,它显示在屏幕上,并且您似乎在循环中声明单元格内容后设置该字段。我有根据的猜测是它无法工作。
答案 3 :(得分:0)
[未经测试] 根据您正在使用的DBMS,您可以在sql语句中添加一个新字段,该字段将是名字和姓氏的串联。 类似的东西:
sqlstatement = "SELECT [First Name] + ' ' + [Last Name] as FullName, [First Name], [Last Name] FROM [EmployeeAccounts]"
' It could be another syntax, it depends on your DBMS
sqlstatement = "SELECT [First Name] || ' ' || [Last Name] as FullName, [First Name], [Last Name] FROM [EmployeeAccounts]"
然后只需将ValueMember和DisplayMember设置为FullName:
comEmployeeName.DisplayMember ="FullName"
comEmployeeName.ValueMember="FullName"
不需要这部分代码:
While x < MaxRows
FirstName = ds.Tables("EmployeeName").Rows(x).Item(0)
LastName = ds.Tables("EmployeeName").Rows(x).Item(1)
FullName(x) = FirstName & " " & LastName
x = x + 1
End While