我在 MS Access 中创建了一个数据库,并将其移植到 VB.NET。
我在代码中完成大部分工作。
我有一个连续的子表单,其中包含一个组合框,该组合框填充了表(tblSubject)中的值。它有一个键列和一个值列。
tblSubject 主题 ID (PK) | 主题
它使用链接主/子字段链接到父表单。
这个用来填写tblChoice的SubjectID,
tblChoice 选择 ID (PK) | 学生证 (FK) | 主题 ID (FK) | 笔记
我决定使用 DataGridView 来显示这些数据。
根据我的研究,我向 DataGridView 添加了一个 DataGridViewComboBoxColumn。
我希望能够将组合框设置为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'