vb.net DataGridViewComboBoxColumn 选定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19456597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridViewComboBoxColumn Selected Value
提问by Alex Hedley
I have created a database in MS Access that I am porting over to VB.NET.
我在 MS Access 中创建了一个数据库,并将其移植到 VB.NET。
I'm doing most of the work in code.
我在代码中完成大部分工作。
I have a continuous SubForm that contains a combobox that is filled with values from a Table (tblSubject). It has a key column and a value column.
我有一个连续的子窗体,其中包含一个组合框,其中填充了来自表 (tblSubject) 的值。它有一个键列和一个值列。
tblSubjectSubjectID (PK) | Subject
tblSubject主题 ID (PK) | 主题
It is linked to the parent Form using the Link Master/Child Fields.
它使用链接主/子字段链接到父表单。
This is used to fill in the SubjectID of the tblChoice,
这个是用来填写tblChoice的SubjectID的,
tblChoiceChoiceID (PK) | StudentID (FK) | SubjectID (FK) | Notes
tblChoiceChoiceID (PK) | 学生证 (FK) | 主题 ID (FK) | 笔记
I decided to use a DataGridView to display this data.
我决定使用 DataGridView 来显示这些数据。
From my research I've added a DataGridViewComboBoxColumn to the DataGridView.
根据我的研究,我向 DataGridView 添加了一个 DataGridViewComboBoxColumn。
I would like to be able to set the combobox to be the value that is in the tblChoicebut am having difficultly working out how. Am I missing something obvious?
我希望能够将组合框设置为tblChoice 中的值,但我很难弄清楚如何。我错过了一些明显的东西吗?
I found a way to set the value to a specific row but I need this to be the value from the table, as each may be different.
我找到了一种将值设置为特定行的方法,但我需要将其作为表中的值,因为每个值可能不同。
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
http://vbcity.com/forums/t/165967.aspx
Do I need to be looking into DataGridViewComboBoxCell?
我需要查看DataGridViewComboBoxCell吗?
'Dim dgvcbc As DataGridViewComboBoxCell = DirectCast(dgvChoices.Rows(0).Cells(0), DataGridViewComboBoxCell)
Code代码
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
Other Links其他链接
I don't need a Default value but found this to be useful.
我不需要默认值,但发现这很有用。
col.DefaultCellStyle.DataSourceNullValue = 1; //this is not index! It is value binded to 'ValueMember'
采纳答案by Steve
You should set the comboboxes DisplayMember and ValueMember to the fields you want on your combobox and then set the DataPropertyName to the field you want to link in the child table.
您应该将组合框 DisplayMember 和 ValueMember 设置为您想要在组合框中显示的字段,然后将 DataPropertyName 设置为您要在子表中链接的字段。
You have most of it, since you are doing SELECT *, I dont know your field names but assuming it links to "SubjectID" in the "tblChoice" table, you would want:
你有大部分,因为你在做SELECT *,我不知道你的字段名称,但假设它链接到“tblChoice”表中的“SubjectID”,你会想要:
.DataPropertyName = "SubjectID"

