Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2015. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: LINQ-to-SQL ComboBox Demo
- '
- ' Author: Joseph L. Bolen
- ' Date Created: Oct 2015
- '
- ' Description: Demonstrate various methods to code LINQ-to-SQL ComboBox
- ' initialization.
- '
- ' Documentation is at:
- ' App's Visual Basic .NET code is at http://pastebin.com/gdZBAVKg
- '-------------------------------------------------------------------------------------------
- Public Class MainForm
- Private RegionBindingSource As New BindingSource
- Private Sub MainForm_Load(sender As Object, e As EventArgs) _
- Handles Me.Load
- ' Prevent event from firing until after the load of the ComboBox.
- RemoveHandler RegionDescriptionComboBox.SelectedIndexChanged,
- AddressOf RegionDescriptionComboBox_SelectedIndexChanged
- '' (1)-----------------------------------------------------------
- '' LINQ-to-SQL using a BindingSource and sorting the BindingSource.
- '' Preferred for a Master/Detail or Parent/Child table
- '' relationship.
- '' --------------------------------------------------------------
- 'Using db As New NorthwindDataContext
- ' RegionBindingSource.DataSource = db.Regions
- 'End Using
- 'RegionBindingSource.Sort = "RegionDescription"
- 'With RegionDescriptionComboBox
- ' .DisplayMember = "RegionDescription"
- ' .ValueMember = "RegionID"
- ' .DataSource = RegionBindingSource
- ' .SelectedIndex = -1
- 'End With
- '' (2)-----------------------------------------------------------
- '' LINQ-to-SQL using a BindingSource and sorting in the query.
- '' Preferred for a Master/Detail or Parent/Child table
- '' relationship.
- '' --------------------------------------------------------------
- 'Using db As New NorthwindDataContext
- ' Dim myQuery = From rec In db.Regions
- ' Order By rec.RegionDescription
- ' Select rec
- ' RegionBindingSource.DataSource = myQuery
- 'End Using
- 'With RegionDescriptionComboBox
- ' .DisplayMember = "RegionDescription"
- ' .ValueMember = "RegionID"
- ' .DataSource = RegionBindingSource
- ' .SelectedIndex = -1
- 'End With
- '' (3)-----------------------------------------------------------
- '' LINQ-to-SQL w/o sorting. No BindingSource.
- '' --------------------------------------------------------------
- 'Using db As New NorthwindDataContext
- ' With RegionDescriptionComboBox
- ' .DisplayMember = "RegionDescription"
- ' .ValueMember = "RegionID"
- ' .DataSource = db.Regions
- ' .SelectedIndex = -1
- ' End With
- 'End Using
- '(4)------------------------------------------------------------
- ' LINQ-to-SQL using sorting. No BindingSource.
- ' --------------------------------------------------------------
- Using db As New NorthwindDataContext
- Dim myQuery = From rec In db.Regions
- Order By rec.RegionDescription
- Select rec
- With RegionDescriptionComboBox
- .DisplayMember = "RegionDescription"
- .ValueMember = "RegionID"
- .DataSource = myQuery
- .SelectedIndex = -1
- End With
- End Using
- AddHandler RegionDescriptionComboBox.SelectedIndexChanged,
- AddressOf RegionDescriptionComboBox_SelectedIndexChanged
- End Sub
- Private Sub RegionDescriptionComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) _
- Handles RegionDescriptionComboBox.SelectedIndexChanged
- RegionIDResult.Text = RegionDescriptionComboBox.SelectedValue.ToString
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment