我在MS Access中创建了一个数据库,我将其移植到VB.NET。
我正在用代码完成大部分工作。
我有一个连续的SubForm,它包含一个填充了Table(tblSubject)值的组合框。它有一个键列和一个值列。
tblSubject SubjectID(PK)|受试者
使用Link Master / Child Fields链接到父Form。
这用于填写tblChoice的SubjectID,
tblChoice ChoiceID(PK)|学生ID(FK)| SubjectID(FK)|备注
我决定使用DataGridView来显示这些数据。
从我的研究中我已经将DataGridViewComboBoxColumn添加到DataGridView。
我希望能够将组合框设置为 tblChoice 中的值,但我很难弄清楚如何。 我错过了一些明显的东西吗?
我找到了一种将值设置为特定行的方法,但我需要将其作为表中的值,因为每个值都可能不同。
For Each dgvRow As DataGridViewRow In dgvChoices.Rows
dgvRow.Cells(SubjectComboBoxColumn.Name).Value = 3 'dgvChoices.Rows.Item
Next
http://vbcity.com/forums/t/165967.aspx
我是否需要查看 DataGridViewComboBoxCell ?
'Dim dgvcbc As DataGridViewComboBoxCell = DirectCast(dgvChoices.Rows(0).Cells(0), DataGridViewComboBoxCell)
<小时/> 的代码
Dim dbProvider As String
Dim dbSource As String
Dim conStudent As New OleDb.OleDbConnection
'Choices
Dim dsChoices As New DataSet
Dim daChoices As OleDb.OleDbDataAdapter
Dim dvChoices As New DataView
Dim sqlChoices As String
'Subjects
Dim dsSubjects As New DataSet
Dim daSubjects As OleDb.OleDbDataAdapter
Dim sqlSubjects As String
Private Sub frmNAME_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" '.mdb
dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;" '.accdb
dbSource = "Data Source=C:\db.accdb"
conStudent.ConnectionString = dbProvider & dbSource
conStudent.Open()
sqlChoices = "SELECT * FROM tblChoice WHERE StudentID=" & txtStudentID.Text
daChoices = New OleDb.OleDbDataAdapter(sqlChoices, conStudent)
daChoices.Fill(dsChoices, "Choices")
'Populate the DataGridView
dvChoices = dsChoices.Tables("Choices").DefaultView
'dgvNotes.DataSource = dsChoices.Tables("Choices")
dgvChoices.DataSource = dvChoices
'Subjects Combo
sqlSubjects = "SELECT * FROM tblSubject"
daSubjects = New OleDb.OleDbDataAdapter(sqlSubjects, conStudent)
daSubjects.Fill(dsSubjects, "Subjects")
Dim SubjectComboBoxColumn As New DataGridViewComboBoxColumn
With SubjectComboBoxColumn
.DataSource = dsSubjects.Tables("Subjects").DefaultView
.DisplayMember = "Subject"
.ValueMember = "SubjectID"
'.DisplayIndex = 0
End With
dgvChoices.Columns.Add(SubjectComboBoxColumn)
conStudent.Close()
End Sub
<小时/> 其他链接
我不需要默认值,但发现这很有用。
col.DefaultCellStyle.DataSourceNullValue = 1; //this is not index! It is value binded to 'ValueMember'
答案 0 :(得分:1)
您应该将组合框DisplayMember和ValueMember设置为组合框中所需的字段,然后将DataPropertyName设置为要在子表中链接的字段。
你有大部分内容,因为你正在做SELECT *
,我不知道你的字段名称,但假设它链接到“tblChoice”表中的“SubjectID”,你会想要:
.DataPropertyName = "SubjectID"