我想知道是否有一个人可以帮助我,我是一名学生,我通常会通过Google搜索来找到解决此类问题的方法,但现在我已经完全迷路了。
我在数据库中有两个表:
PERSONS PHONES
+----+-------+ +-----------+----------+
| ID | Name | | Person_ID | Phone |
+----+-------+ +-----------+----------+
| 1 | John | | 1 | 47281923 |
| 2 | Paul | | 1 | 92145694 |
| 3 | Chris | | 2 | 12345678 |
+----+-------+ | 3 | 83929171 |
+-----------+----------+
因此,为了获得所有人的所有电话,我必须执行这句话:
SELECT name, phone FROM Persons, Phones WHERE Persons.ID = Phones.Person_ID
因此该SQL查询的输出应为:
+-------+----------+
| Name | Phone |
+-------+----------+
| John | 47281923 |
| John | 92145694 |
| Paul | 12345678 |
| Chris | 83929171 |
+-------+----------+
到目前为止,这么好。问题是我在DataGridView(VB.NET)中显示结果并且我希望手机在DataGridView的单元格中填充ComboBox,因此DataGridView将类似于:
+-----+----------+------------------------------------------------+
| ROW | NAME | PHONE |
+-----+----------+------------------------------------------------+
| 1 | John | (ComboBox, with phones 47281923 and 92145694) |
| 2 | Paul | (ComboBox, with phone 12345678) |
| 3 | Chris | (ComboBox, with phone 83929171) |
+-----+----------+------------------------------------------------+
我知道我可以创建一个DataGridViewComboBoxColumn,并为该列分配一个DataSource,但这将是整个列的DatasSource,而不是每行的每个“Phone cell”。 < / p>
如何实现这一目标?我正在使用Visual Basic,使用Windows Forms,使用ODBC和Informix数据库来存储/获取数据。
我试过这个,其中一个答案的修改版本,所以我可以从表而不是列表中添加数据:
For i = 0 To dgvPersons.Rows.Count
'The cell with index 1 has the IDNumber of that person.
Dim PersonID As String = connection.ExecuteSelect("SELECT PersonID FROM Persons where IDNumber = '" + dgvPersons.Rows(i).Cells(1).Value.ToString() + "'").Rows(0)(0).ToString()
'It seems I need to convert the Phone to Varchar(20) in the query because the phone is a BigInt and I get an error if I do not convert it.
Dim PersonPhones = connection.ExecuteSelect("SELECT phone::varchar(20) phone from Phones where PersonID = '" + PersonID + "'")
setCellComboBoxItems(dgvClientes, i, 7, TelefonosCliente)
Next
Private Sub setCellComboBoxItems(dataGrid As DataGridView, rowIndex As Integer, colIndex As Integer, itemsToAdd As DataTable)
Dim dgvcbc As DataGridViewComboBoxCell = DirectCast(dataGrid.Rows(rowIndex).Cells(colIndex), DataGridViewComboBoxCell)
For Each row As DataRow In itemsToAdd.Rows
dgvcbc.Items.Add(row.Item(0))
Next
End Sub
当我这样做时,程序需要永远加载(我有105行)并且我在ComboBox中没有项目,即使我点击ComboBox,它也没有做任何事情。
executeSelect函数在另一个类中,我写了它。
“IDNumber”是标识此人的数字。 IDNumber是唯一键,但PersonID是串行主键。我只在DataGridView中显示IDNumber,但IDNumber存在于数据库表中。
我的DataGridView已经填充了Persons表的属性,因此已经设置了DataGridView本身的DataSource。我想如果是这样的话,我无法更改Phone列的内容?
也许有办法创建一个允许DataGridView自动放置ComboBox的绑定源?