Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NPS-ARCN-CAKN/74034dd2b5ee3f6653bd727dd725289e to your computer and use it in GitHub Desktop.
Save NPS-ARCN-CAKN/74034dd2b5ee3f6653bd727dd725289e to your computer and use it in GitHub Desktop.
VB .NET: Set up a data bound, code translating DataGridViewComboBox
Private Sub LoadWorkLogNetworkIDComboBox()
'get a reference to the datagridview's combobox column
Dim NetworkIDCombobox As DataGridViewComboBoxColumn = Me.TblNetworkWorkLogDataGridView.Columns("WorkLog_NetworkIDComboBox")
'set the combobox's datapropertyname to the corresponding column in its bindingsource. this bindingsource was set up using the form's tools, not in code
With NetworkIDCombobox
'bind the combobox to the NetworkID column of the work log bindingsource's datatable
.DataPropertyName = "NetworkID"
'load the combobox options from a database query
Dim AKROSqlConnection As New SqlConnection(My.Settings.AKROConnectionString)
Dim NetworksLookupSqlDataAdapter As New SqlDataAdapter("SElECT NetworkID, Acronym FROM tblNetworks", AKROSqlConnection)
Dim NetworksLookupDataTable As New DataTable("NetworksLookup")
NetworksLookupSqlDataAdapter.Fill(NetworksLookupDataTable)
Dim NetworksLookupBindingSource As New BindingSource
NetworksLookupBindingSource.DataSource = NetworksLookupDataTable
'load the combobox options with the records from the lookup datatable and translate the NetworkID code to an Acronym
.DataSource = NetworksLookupBindingSource
.ValueMember = "NetworkID" 'the actual value
.DisplayMember = "Acronym" 'the virtual value the user will see
End With
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment