Pergunta

I have created a database in MS Access that I am porting over to 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 SubjectID (PK) | Subject

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 ChoiceID (PK) | StudentID (FK) | SubjectID (FK) | Notes

I decided to use a DataGridView to display this data.

From my research I've added a DataGridViewComboBoxColumn to the DataGridView.

I would like to be able to set the combobox to be the value that is in the tblChoice but am having difficultly working out how. Am I missing something obvious?


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


Do I need to be looking into 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'

selecting value from DataGridViewComboBoxColumn?

Foi útil?

Solução

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.

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:

.DataPropertyName = "SubjectID"
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top