我有一个在Postgresql中完美执行的查询:
SELECT
matchid
, f1.firstname
, f1.lastname
, f2.firstname
, f2.lastname
FROM matches AS m
INNER JOIN fighters AS f1
ON f1.fighterid = m.fighteroneid
INNER JOIN fighters AS f2
ON f2.fighterid = m.fightertwoid
它显示了战斗机的一行,但是当我尝试在组合框中显示结果时,它显示为“System.Data.DataRow”而不是数据。
VB.NET中的代码:
mySelectQuery = "SELECT f1.firstname, f1.lastname, f2.firstname, f2.lastname FROM matches AS m INNER JOIN fighters AS f1 ON f1.fighterid = m.fighteroneid INNER JOIN fighters AS f2 ON f2.fighterid = m.fightertwoid"
pgAdapter = New PgSqlDataAdapter(mySelectQuery, pgConnection)
pgAdapter.Fill(dtMatches)
With cboMatches
.DisplayMember = "fighters"
.ValueMember = "matchid"
.DataSource = dtMatches
.SelectedIndex = -1
End With
我试图使用fighters,m,f1,f2作为显示成员,它总是返回相同的输出。
如果我将.DataSource移动到.DisplayMember之上,我会收到错误:无法绑定到新的显示成员。参数名称:newDisplayMember。
提前谢谢你,
彭斯
答案 0 :(得分:2)
ValueMember和DisplayMember应引用数据源的列名 您没有指定名称的任何内容 您可以尝试为DisplayMember指定一个名称,但您的查询不会生成任何MatchID。
我不是PostegreSQL的专家,但您可以尝试以下aggregate the names
mySelectQuery = "SELECT m.MatchID, string_agg(f1.firstname, f1.lastname, ' - ', " +
"f2.firstname, f2.lastname, ' ') as Fighters " +
"FROM matches AS m INNER JOIN fighters AS f1 ON f1.fighterid = m.fighteroneid" +
" INNER JOIN fighters AS f2 ON f2.fighterid = m.fightertwoid"
然后使用
With cboMatches
.DisplayMember = "Fighters"
.ValueMember = "MatchID"
.DataSource = dtMatches
.SelectedIndex = -1
End With
答案 1 :(得分:2)
我最终使用COALESCE,因为string_agg不适用于没有CASTS的名称类型。
SELECT m.MatchID, COALESCE(f1.firstname || ' ' || f1.lastname || ' - ' || f2.firstname || ' ' || f2.lastname) as Fighters
FROM matches AS m INNER JOIN fighters AS f1 ON f1.fighterid = m.fighteroneid
INNER JOIN fighters AS f2 ON f2.fighterid = m.fightertwoid